php mysql_num_rows() 期望参数 1 是资源,布尔值在 C:\wamp\www 中给出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17101147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:15:58  来源:igfitidea点击:

mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www

phpmysql

提问by user2446626

The code is working fine with few pages and shows some sort warning like "mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www"... it always confuses me because it is working for some page and at some point shows a warning message which frustrates me a lot...help please...

该代码在几页上运行良好,并显示了某种警告,例如“mysql_num_rows() 期望参数 1 为资源,布尔值在 C:\wamp\www 中给出”......它总是让我感到困惑,因为它适用于某些页面并且在某些时候显示一条警告消息,这让我很沮丧......请帮助......

<?php 
    $emp_id=$_SESSION['username'];
    $s=mysql_query("select semester from faculty_advisor where emp_id = '$emp_id' ");
    echo $s; //just to check if am getting the correct value
    $subject=mysql_query("select * from subject_list where semester=$s");
    $numrows=mysql_num_rows($subject);
    if($numrows!=0)
    {
        while($row=mysql_fetch_array($numrows))
        {
            ?>
        <td align="center"><?php echo  $row['sid']; ?></td>
        <td align="center"><?php echo  $row['subject_name']; ?></td>
    <td align="center"><?php echo  "<a class='tooltip' href=\"fac_adv_reg_confrm.php?uid=".$row['sid']."\">Edit<span>Edit User Details</span></a>"?></td>  
            <?php
        }
    }
    else
    {
   die('Error: ' . mysql_error());
    }

    ?>

回答by Goutam Pal

$s=mysql_query("select semester from faculty_advisor where emp_id = 'macs410' ");
echo $s;
$subject=mysql_query("select * from subject_list where semester=$s");

Should be

$s=mysql_query("select semester from faculty_advisor where emp_id = 'macs410' ");
$row = mysql_fetch_assoc($s); 
$subject=mysql_query("select * from subject_list where semester='".$row['semester']."'");

回答by DevZer0

This is happening because your $subject=mysql_query("select * from subject_list where semester=$s");returned FALSEdue to the query failing, use mysql_error()to find out the error.

发生这种情况是因为您因查询失败而$subject=mysql_query("select * from subject_list where semester=$s");返回FALSE,用于mysql_error()找出错误。

Usually do mysql_query() or die (mysql_error())to ensure the code doesn't continue after an error.

通常这样做是mysql_query() or die (mysql_error())为了确保代码在出错后不会继续。

UPDATE

更新

i also noticed that in your first query result $syour using directly as if its a variable. you need to extract the row using mysql_fetch_assocor mysql_fetch_rowbefore passing it to the second query

我还注意到,在您的第一个查询结果中,$s您直接将其用作变量。您需要使用mysql_fetch_assocmysql_fetch_row在将其传递给第二个查询之前提取该行

回答by Goutam Pal

 $s=mysql_query("select semester from faculty_advisor where emp_id = 'macs410' ");
 echo $s;
 $subject=mysql_query("select * from subject_list where semester=$s");

Hear $s is a resource object so 
mysql_query("select * from subject_list where semester=$s");
will give an error

hence $subject is not a valid resource object