数据库连接错误

时间:2013-11-19 05:25:19

标签: php database

在下面的代码中,当我通过$id_num检查数据库中的id字段时,它接受但是当我想传递用户ID以检查数据库时,它显示以下错误;

  

警告:mysqli_fetch_array()要求参数1为mysqli_result,第12行给出布尔值

谁能告诉我哪里出错了。

if(isset($_POST['user_mail']) && isset($_POST['user_pass']))
{
    $var_1=$_POST["user_mail"];
    $var_2=$_POST["user_pass"];
    $result = mysqli_query($con,"SELECT * FROM jsrao_db2 WHERE user_mail=$var_1");
    while($row = mysqli_fetch_array($result))
    {
            if(($row['user_mail']==$var_1) && ($row['user_pass']==$var_2))//compare user name and password with database value
            echo "Welcome";
            else
            echo "Try Again";
    }

2 个答案:

答案 0 :(得分:2)

更改您的查询

$result = mysqli_query($con,"SELECT * FROM jsrao_db2 WHERE user_mail=$var_1");<br>

应该是

$result = mysqli_query($con,"SELECT * FROM jsrao_db2 WHERE user_mail='$var_1'");<br>

user_mail是一个字符串,因此在$ var_1'

中包含$ var_1

答案 1 :(得分:1)

使用Prepared Statements清理代码:

$result = false;
$stmt = $con->prepare("SELECT * FROM jsrao_db2 WHERE user_mail=?");
$stmt->bind_result($result);
$result = $stmt->bind_param("s", $var_1)->execute();
if ($result) {
    //work with $result
}