mysql_fetch_assoc()问题

时间:2012-03-28 16:30:52

标签: php

  

可能重复:
  Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

我收到此错误消息:

  

警告:mysql_fetch_assoc():提供的参数不是第215行/.../.../..../index.php中有效的MySQL结果资源

Index.php代码:

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']); 

在我的Localhost中,它没关系,它没有显示任何错误消息但是当它到我的服务器然后它显示错误.. 我的代码有什么问题?任何人都可以告诉我正确的方向...... 非常感谢。

2 个答案:

答案 0 :(得分:4)

您在以下行中有错误

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

函数mysql_error()返回一个字符串,因此当mysql_query()失败时,$sql变量将包含抛出的任何错误。稍后,您将此字符串变量作为MySQL资源。

相反,您可能尝试做的是打印出任何字符串mysql_error()返回并终止脚本。这通常在这些脚本中使用die()来完成。

因此,如果您使用以下内容,脚本将终止并输出错误,如果发生错误。

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

最后,您声称上述内容适用于您的服务器,但不适用于localhost,这是不正确的。如果您的服务器上发生错误,您将获得相同的行为。

答案 1 :(得分:0)

这意味着您的查询出现了问题,但是您在代码中处理错误。这意味着查询不会给出结果(受影响的行= 0)

if(mysql_affected_rows($sql) > 0) { 
   //its ok we have result
}
else {
  //mo result found
}
相关问题