1

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

I'm getting this error message:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /.../.../..../index.php on line 215

Index.php code:

if (isset($_GET['post_id']) && $_GET['post_id'] != '')
{
$p_id = (int) $_GET['post_id'];
$sql= mysql_query("SELECT * FROM news_post WHERE post_id = '$p_id' AND block = 0 LIMIT 
$offset, $rowsperpage") or mysql_error();
}
else 
{
$sql= mysql_query("SELECT * FROM news_post WHERE block = 0 ORDER BY  post_id DESC 
LIMIT $offset, $rowsperpage ") or mysql_error();
}
while ($rel = mysql_fetch_assoc($sql))      (Note: this is Line 215)
{
$id = intval($rel['post_id']);
$sub = ucfirst($rel['subject']);
$imgname = htmlentities($rel['img_name']);
$img = htmlentities($rel ['image']);
$msg = $rel['message'];
$date = htmlentities($rel['date']);
$poster = htmlentities($rel['poster']);
$cat_id = intval($rel['cat_id']);
$cat_name = htmlentities($rel['cat_name']); 

In my Localhost, it's Ok, it's doesn't show any error message but when up this to my server then it's show error.. What is wrong in my code? can anyone tell me the right direction...
Thanks a lot.

Community
  • 1
  • 1

2 Answers2

4

You have an error in the following line

$sql= mysql_query("SELECT * FROM news_post WHERE block = 0 ORDER BY  post_id DESC
                   LIMIT $offset, $rowsperpage ") or mysql_error();

The function mysql_error() returns a string, so when mysql_query() fails, the $sql variable will contain whatever error was thrown. Later, you tread this string variable as a MySQL resource.

Instead, what you're probably trying to do, is to print out whatever string mysql_error() returns and terminate the script. This is usually accomplished with die() in these kinds of scripts.

So, if you instead use the following, the script will terminate and output the error, should one occur.

$sql= mysql_query("SELECT * FROM news_post WHERE block = 0 ORDER BY  post_id DESC
                   LIMIT $offset, $rowsperpage ") or die(mysql_error());

Finally, you claim that the above works on your server, but not on localhost, which is not correct. If an error occured on your server, you would get the same behavior.

kba
  • 19,333
  • 5
  • 62
  • 89
0

it means your there something wrong on your query but , you handling errors in your code. That means query doesnt give result (affected rows = 0)

if(mysql_affected_rows($sql) > 0) { 
   //its ok we have result
}
else {
  //mo result found
}
safarov
  • 7,793
  • 2
  • 36
  • 52