close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

PHP Join displaying too much data

Thread began 8/25/2009 11:15 pm by 360876 | Last modified 9/02/2009 10:43 am by Jason Byrnes | 4987 views | 10 replies |

360876

PHP Join displaying too much data

I really need help here! I'm not an idiot - but there's not way I can prove it on this one. This whole "join" or "inner join" concept is a real hair puller! I have spent way too much time trying to figure this out on my own form w3schools to php.net to phpfreaks . . . I just can't figure it out.

I am using the BlueSky schema. On my details page (created with DataAssist), I want to display the GroupName as opposed to the GroupID that is stored in the visitors table.

The following code appeared as if it were going to work fine until I added the second user.

<?php
$query = "SELECT visitors.VisitorGroupID, groups.GroupName ".
"FROM visitors, groups ".
"WHERE visitors.VisitorGroupID = groups.GroupID";

$result = mysql_query($query) or die(mysql_error());

// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
echo $row['GroupName'];
echo "<br />";

}
?>

After adding the second user, the code above "echo"d' the GroupName for all rows in the table. In this case both users belong to the "Members" category and my details page had "Members" listed twice (one for each of the two members).

How do I look up the correct GroupName, and only the GroupName that applies to the user being queried? Thanks alot!

Jim

Sign in to reply to this post

Jason ByrnesWebAssist

Your query needs an additional where clause to filer on the VisitorID column.

You how are you determining which user is being queried?

Are you passing a querystring value with the VisitorID?

Is this a logged in user where there is a session variable to filter on?


If you are using security assist, there will be a session variable set that contains the logged in users Primary Key, it will probably be named VisitorID. You could modify your SQL To filter the visitor ID column on the session variable:

if(!session_id()) ssession_start();
$query = "SELECT visitors.VisitorGroupID, groups.GroupName ".
"FROM visitors, groups ".
"WHERE visitors.VisitorGroupID = groups.GroupID AND visitors.visitorID = ". isset($_SESSION['visitorID'])?$_SESSION['visitorID']:"-1";

Sign in to reply to this post

360876

Thanks for your help and patience. You've helped me before and I know you know your stuff; unfortunately, I can't say the same for me.
- - - -
No, the user isn't logged in here; the administrator is. Although I have many pages where I need to do a similar join, this specific page is my admin_visitorDetail page.

The two tables are:

visitors
VisitorID
VisitorGroupID

groups
GroupID
GroupName

The details page is showing the data from the visitors table which means for visitors.VisitorGroupID - it displays groups.GroupID. I want it to display the GroupName.

The code I have above (not that I'm married to it) shows the groups.GroupName ok - but it shows all of them from every record in the groups table. I just need it to return the single GroupName for the GroupID that is stored in the visitors.VisitorGroupID column.

Sign in to reply to this post

Jason ByrnesWebAssist

OK. Perhaps try using a GROUP BY clause:
$query = "SELECT visitors.VisitorGroupID, groups.GroupName ".
"FROM visitors, groups ".
"WHERE visitors.VisitorGroupID = groups.GroupID GROUP BY groups.GroupName";

Sign in to reply to this post

360876

Does this need an echo statement as well? I copied this code in place of the same statement in the original php string. It didn't work - in fact it produced an error and referenced the last line on the page which is ?> - a closing php code.

Perhaps I'm doing something wrong. I've spent hours online trying to learn how to put a join string together. The tutorials don't seem to show any way to limit the response to just one value - everything I've found point to combining "tables" of data.

Not sure where to go from here.

Sign in to reply to this post

Jason ByrnesWebAssist

Post the exact text of the error message and a copy of the page so I can debug it.

Sign in to reply to this post

360876

Here are the some screen shots - the page didn't produce an error - it just didn't produce a result at all where I put the new code (it's in the main table). Here are some screen shots of the tables and the results.

1. WA_tables.gif are the tables I'm using
2. WA_tables_results are what the pages look like (you'll see the duplicate "Member" on the Profile and Visitor Detail screen shot.

I have this posted on a test URL and would be happy to email you the URL with a login & password if you want to look at it live (I don't want to include that info here).

Here's the old (top) and new (bottom) code (I don't see how to upload the page). This is what produce the double "Member" on the "Visitor Details" (screenshot):

<td class="tableHeader">Access Level:</td>
<td class="tableData"><?php
$query = "SELECT visitors.VisitorGroupID, groups.GroupName ".
"FROM visitors, groups ".
"WHERE visitors.VisitorGroupID = groups.GroupID";
$result = mysql_query($query) or die(mysql_error());

// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
echo $row['GroupName'];
echo "<br />";

}
?></td>
</tr>
<tr>
<td><?php
$query = "SELECT visitors.VisitorGroupID, groups.GroupName ".
"FROM visitors, groups ".
"WHERE visitors.VisitorGroupID = groups.GroupID GROUP BY groups.GroupName";
?>

Sign in to reply to this post

Jason ByrnesWebAssist

You need to change:
<td class="tableHeader">Access Level:</td>
<td class="tableData"><?php
$query = "SELECT visitors.VisitorGroupID, groups.GroupName ".
"FROM visitors, groups ".
"WHERE visitors.VisitorGroupID = groups.GroupID";
$result = mysql_query($query) or die(mysql_error());

// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
echo $row['GroupName'];
echo "<br />";

}
?></td>
</tr>
<tr>
<td>




to:
<td class="tableHeader">Access Level:</td>
<td class="tableData"><?php
$query = "SELECT visitors.VisitorGroupID, groups.GroupName ".
"FROM visitors, groups ".
"WHERE visitors.VisitorGroupID = groups.GroupID GROUP BY groups.GroupName";
$result = mysql_query($query) or die(mysql_error());

// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
echo $row['GroupName'];
echo "<br />";

}
?></td>
</tr>
<tr>
<td>

Sign in to reply to this post

360876

Fixed - thank you!

Per your usual - it works like a charm!

I really appreciate your (all WebAssist) world-class service!

Sign in to reply to this post

jshafor

Oops - I spoke too soon . . .

Hey Jason - I spoke too soon. It's almost working the way we need it to . . . one slight glitch still exists.

It's pulling data from the proper columns - so that part is correct. However, it's not pulling the Access Level for the specific record only; it's pulling the entire list of unique "Access Level" names for all registered member/administrators.

Example: I have two "members" signed up. When they're both set to an "Access Level" of "member" - only "member" shows up next to each name under "Access Level" (which is why I thought we had it right when I sent my last post). When I changed the "Access Level" to "member" for one and "administrator" for the other, it then showed "Administrator" and "Member" in the results table next to each record - essentially displaying ALL unique "Access Levels" that are contained in the visitors table.

I just want to return the result for the single record that has an "Access Level" assigned to in - just one group.GroupName per registered member/administrator.

Thanks in advance again - I appreciate the help.

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...