Jump to content

[SOLVED] PHP/MySQL/ Join Query


jreed2132

Recommended Posts

I have the following code:

 

$query = "Select p.ProfileID, p.ProfileName from Profiles as p join ProfileAccess as a on p.ProfileID = a.ProfileID Order By p.ProfileName;";

 

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

$row = mysql_fetch_array($result) or die(mysql_error());

 

while($row = mysql_fetch_array($result))

{

echo "<a href = viewprofile.php?info=$row[ProfileID]>$row[ProfileName]</a>";

echo "<br />";

}

 

mysql_close();

 

 

When the script runs, I get nothing.  No SQL errors or anything.  I even changed the query so that it would error and I receive one.

 

I take that same query and run it directly in MYSQL and it works.  I also changed the query in my script to a basic, "Select * from Profiles", and the script works.

 

Why won't this run in the PHP script?

Link to comment
Share on other sites

Why did a plain old "Select * from Profiles" but not the join?  If one worked, why wouldn't the other?

 

Because select * from profiles select's all profiles while select p.profileid, p.profilename from profiles as p join profileaccess as a on p.profileid = a.profileid order by p.profilename select's only one matching row. Which is retrieved by:

 

$row = mysql_fetch_array($result);

 

$row = array('profileid' => .., 'profilename' => ..);

 

and overwritten by:

 

while ($row = mysql_fetch_array($result))

 

$row = false;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.