Jump to content

[SOLVED] PHP and MYSQL - Please help!!!!!!


FireDrake

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  :)

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.