-2

该代码在几页上运行良好,并显示某种警告,例如“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());
    }

    ?>
4

3 回答 3

3
$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']."'");
于 2013-06-14T04:50:20.993 回答
1

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

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

更新

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

于 2013-06-14T04:37:03.643 回答
0
 $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 
于 2013-06-14T04:37:35.890 回答