PHP警告:mysql_fetch_assoc()期望参数1是资源,给定布尔值

时间:2015-08-03 11:05:45

标签: php

网页不会死,它只显示警告。 请告诉我这段代码中的问题。谢谢。

<?php

$connect = mysql_connect('localhost','root','') || die("Error");
$selectDB = mysql_select_db('mydb') || die("Error");

$query = "SELECT * FROM user_login WHERE ID='2';";
$fetch = mysql_query($query) || die("Error");

while ($row = mysql_fetch_assoc($fetch)){ #problem is somewhere here
    echo $row['User_Name'];
    echo $row['Password'];
    echo $row['Gender'];
    echo $row['ID'];
}

?>

给出的警告是:

  

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

1 个答案:

答案 0 :(得分:0)

<?php
    $connect = mysql_connect('localhost','root','') || die("Error");
    $selectDB = mysql_select_db('mydb') || die("Error");

    $query = "SELECT * FROM user_login WHERE ID='2';";

    $fetch = mysql_query($query) || die("Error");
    $count = count($fetch);

    if($count>0)
    {
        while ($row = mysql_fetch_assoc($fetch))
        { #problem is somewhere here
            echo $row['User_Name'];
            echo $row['Password'];
            echo $row['Gender'];
            echo $row['ID'];
        }
    }
    else
    {
        echo 'No Item to select';
    }
?>
  

警告
  自PHP 5.5.0起,此扩展已弃用,将来将被删除。相反,应使用MySQLiPDO_MySQL扩展名。有关详细信息,另请参阅MySQL: choosing an API指南和相关常见问题解答。