...

View Full Version : I get this often, dunno why...



martynball
03-02-2010, 09:10 PM
Error:


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a9012858/public_html/scripts/getReview.php on line 14


PHP:


<?php
include "connect.php";

//Connect to database
mysql_select_db($dbname, $con);

//Get data from database for current user
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM comments WHERE id=$id");
/*if (!result) {
echo "No reviews yet!";
}*/

while ($row = mysql_fetch_array($result)) {
//Variables to echo out data
$name=$row['name'];
$message=$row['message'];
$date=$row['date'];
$id=$row['id'];

//Display results
echo
"<div class=\"comment\">
<div class=\"commentDiv\">Posted by $name</div>
<div class=\"message\">$message</div>
<div class=\"commentDiv\">Posted on $date</div>
</div>";
}
?>


I don't what is wrong with it....

bacterozoid
03-02-2010, 09:14 PM
It means that the result resource you're trying to pull a row from isn't actually a result.

Change this:


$result = mysql_query("SELECT * FROM comments WHERE id=$id");

To this:


$result = mysql_query("SELECT * FROM comments WHERE id=$id") or die(mysql_error());

and rerun your code. That will tell you if your query failed and why.

martynball
03-02-2010, 09:17 PM
Cheers :)

JAY6390
03-03-2010, 06:00 PM
It's also a good idea to wrap all of the values you use in single quotes for things like $id in your example
$result = mysql_query("SELECT * FROM comments WHERE id='$id'");
This means if your id accidentally has a string instead of a numeric value it's not going to throw an error about your query, it just won't return a result



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum