jreed2132 Posted July 29, 2009 Share Posted July 29, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/167962-solved-phpmysql-join-query/ Share on other sites More sharing options...
ignace Posted July 29, 2009 Share Posted July 29, 2009 $row = mysql_fetch_array($result) or die(mysql_error()); while($row = mysql_fetch_array($result)) You are calling mysql_fetch_array() twice. P.S. remove 'or die()' your users don't know what 'SQL syntax error near ..' means. Quote Link to comment https://forums.phpfreaks.com/topic/167962-solved-phpmysql-join-query/#findComment-885894 Share on other sites More sharing options...
jreed2132 Posted July 29, 2009 Author Share Posted July 29, 2009 Ahh, Great, thanks, works now, Why did a plain old "Select * from Profiles" but not the join? If one worked, why wouldn't the other? Quote Link to comment https://forums.phpfreaks.com/topic/167962-solved-phpmysql-join-query/#findComment-885896 Share on other sites More sharing options...
ignace Posted July 29, 2009 Share Posted July 29, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/167962-solved-phpmysql-join-query/#findComment-885906 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.