【问题标题】:Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given on line 26 [duplicate]警告:mysql_num_rows() 期望参数 1 是资源,第 26 行给出的布尔值 [重复]
【发布时间】:2012-12-20 01:13:21
【问题描述】:

可能重复:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given

我正在尝试启动我的论坛系统,但我不断收到此错误:

警告:mysql_num_rows() 期望参数 1 是资源,第 26 行给出的布尔值

大部分代码只出现一个错误。这是我的完整代码:

<?php
include_once "../ads/connect_to_mysql.php";//Connect to Database

//Get section ID from the url varible coming in
if(isset($_GET['id'])&&$_GET[id]!=""){
    $sid=preg_replace('#[^0-9]#i','',$_GET['id']); //filter all characters except
}else{
    echo "ERROR: Variables to run this script have been removed from the URL";
    exit();
}
//Query the database for that section id, make sure it exists and get section title
$sql=mysql_query("SELECT*From ad_sections WHERE id='$sid' LIMIT 1");
$numRows=mysql_num_rows($sql);
if($numRows<1){
    echo "ERROR: That section does not exist, you have tampered with our URLs.";
    exit();

}
while($row=mysql_fetch_array($sql)){
    $sectionTitle=$row["title"];

}
//Use the section ID to query a "ad_posts" table in the database to get all the threads for this section
$sql=mysql_query("SELECT*From ad_sections WHERE type='a'AND section_id='$sid' ORDER BY date_time DESC LIMIT 25");  
$dynamicList="";
$numRows=mysql_num_rows($sql);
if($numRows<1){
    $dynamicList="There are no classifieds in this section yet. You can be the first to post.";
}else{
    while($row=mysql_fetch_array($sql)){
    $thread_id=$row["id"];
    $thread_Title=$row["thread_title"];
    $dynamicList.='<a href="view_thread.php?id=' . $thread_id . '">' . $thread_Title . '</a><br/>';
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html, charset=utf-8"/>
<title><?php echo "title"; ?></title>
<style>
</style>
</head>
<body>
<table style="bacground-color: #f0f0f0; border:#069 1px solid; border-top:none; width="900">
<tr>
<td width="731"><h2><?php echo $sectionTitle; ?><h2><?php echo $dynamicList; ?></td>
<td width="189">&nbsp;</td>
</tr>
</table>
</body>
</html>

这里是第 26 行,真的是碰壁了:

$numRows=mysql_num_rows($sql);

【问题讨论】:

  • 您没有按照manual. 中所述使用mysql_error() 进行正确的错误检查出于好奇,请问是否有启发此代码的教程?因为我们相对经常收到这些问题,而且我一直认为那里的某个地方有一个损坏的教程

标签: php mysql boolean


【解决方案1】:

关于你的$sql 有错误

试试这个

    $sql=mysql_query("SELECT * From ad_sections WHERE id='$sid' LIMIT 1");
                            ^-^-----spaces here

【讨论】:

  • 不一定,但一直都是这样。
【解决方案2】:

阅读the manual

mysql_query() 成功返回资源,错误返回 FALSE。

...

如果用户不这样做,mysql_query() 也会失败并返回 FALSE 有权访问查询引用的表。

【讨论】:

    猜你喜欢
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    相关资源
    最近更新 更多