警告:mysql_fetch_row()/ mysql_fetch_result()期望参数1是资源,给定布尔值

时间:2013-06-10 09:30:26

标签: php mysql

我是新手。即使我检查了php.net的功能mysql_fetch_row()mysql_fetch_result()我一直收到警告

  

期望参数1为资源,布尔值为

  <?php

 //code

 $sql1="SELECT * FROM $tblname WHERE USERNAME='$myusername' and    PASSWORD='$mypassword'";
 $result1=mysql_query($sql1);

 // Mysql_num_row is counting table row
 $count=mysql_num_rows($result1);
 // If result matched $myusername and $mypassword, table row must be 1 row

 if($count==1)
 {

 $sql2="SELECT CLIENT_ID FROM $tblname WHERE USERNAME='$myusername' and PASSWORD='$mypassword'";
 $result2=mysql_query($sql2); //THIS LINE!!!
 $cid=mysql_result($result2,$row2); //THIS LINE!!!

 // Register info
 $_SESSION['sessname'] = "$myusername";
 $_SESSION['sesspwd'] = "$mypassword";
 $_SESSION['sessid'] = "$cid";
 $_SESSION['sessaccid'] = "$accid";

//more code
 ?>

1 个答案:

答案 0 :(得分:1)

$result2返回一个布尔值(意味着你的选择中发生了错误),因此,你将这个布尔值传递给mysql_result()而不是资源。

这是返回值,取自documentation

  

对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他语句返回   resultset,mysql_query()在成功时返回资源,或者返回FALSE   错误。

您应该修改您的代码,如下所示

[...]
$sql2="SELECT CLIENT_ID FROM $tblname WHERE USERNAME='$myusername' and PASSWORD='$mypassword'";
$result2=mysql_query($sql2);
if($result2)
{
  $cid=mysql_result($result2,$row2); //THIS LINE!!!
}
else
{
//do some error handling
}
[...]

此外,您不应使用mysql_*函数,因为已弃用。请改用PDOmysqli_*