FireDrake Posted November 11, 2007 Share Posted November 11, 2007 I tried to get something out of a database while the loop is still on, so i have a table with links called link(lid, link, image) I'm using a mysql query in a: while ($row = $query){ $row = $some other query to get the links out of the other table }, But ill get an error when I do so: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in E:\Program Files\wamp\www\Index.php on line 1739 Here is the code, please have a look at it (only posting the while part) while ($rij = mysql_fetch_array ($result)) { echo "<br /><br /><table><tr><td>Nummer ".$nummer.": <tr /><td><b>Wedstrijd nummer: </b><td><b>".$rij ['poll_ant']."</b>"; $query = "SELECT * FROM link WHERE lid ='$rij ['poll_ant']'"; $result = mysql_query ($query); $rij = mysql_fetch_array ($result); echo "<tr><td><a href=".$rij ['link'].">Bekijk wedstrijd</a><tr></table>"; $nummer++; } as you can see, I use $rij ['link'] out of the link table, and the lid (link id) should be $rij ['poll_ant'] out of the other table. anyway to solve this in php or mysql way? Quote Link to comment Share on other sites More sharing options...
Daukan Posted November 11, 2007 Share Posted November 11, 2007 change this $query = "SELECT * FROM link WHERE lid ='$rij ['poll_ant']'"; To $query = "SELECT * FROM link WHERE lid ='".$rij['poll_ant']."'"; Quote Link to comment Share on other sites More sharing options...
FireDrake Posted November 11, 2007 Author Share Posted November 11, 2007 doh thats a stupid mistake Thanks for finding it =) - solved Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 11, 2007 Share Posted November 11, 2007 Also, learn to properly escape table and field names. This will prevent any future collision with MySQL reserved words. $query = "SELECT * FROM link WHERE lid ='".$rij['poll_ant']."'"; becomes $query = "SELECT * FROM `link` WHERE `lid` ='".$rij['poll_ant']."'"; Rule of thumb is: Use backticks around table names and field names, single quotes around string values, nothing around integer values. So, for instance, ORDER BY is reserved by MySQL. If you had a field in your table named order, this query will fail: $query = "SELECT * FROM mytable WHERE order > '$some_var'"; however $query = "SELECT * FROM `mytable` WHERE `order` > '$some_var'"; will work just fine. "But I don't have any fields or table names that conflict with MySQL reserved words!" Cool, but MySQL may decide to grab a few more names and reserve them in the future, which means your script might break in a year or two after a MySQL update. Proper formatting of your code is essential to being a programmer. PhREEEk Quote Link to comment Share on other sites More sharing options...
FireDrake Posted November 11, 2007 Author Share Posted November 11, 2007 thanks, I'll change all of my query's right away I still had to do different names for my $var 's because I had already a $query, $result and $rij in the while loop, so I had to change the ones IN the while loop to $query1, etc. Thanks for helping me Quote Link to comment 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.