php脚本无法正常工作并显示有关mysqli的错误

时间:2014-09-29 17:50:22

标签: php mysqli

此脚本正在尝试计算名为qstns的表中的条目数:

<?php
    $con=mysqli_connect("localhost","root","","QSTNS");
    if (mysqli_connect_errno()) {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    //-----------------two variables entered in database------------
    $user= mysqli_real_escape_string($con, $_POST['user']);
    $user1= mysqli_real_escape_string($con, $_POST['user1']);

    $sql="INSERT INTO student (name,rln,scr)VALUES ('$user1','$user','0')";
    if (!mysqli_query($con,$sql)) {
       die('Error: ' . mysqli_error($con));
    }

    $res=mysqli_query('SELECT COUNT(*) FROM qstns', $con);
    echo "$res \n";
    mysqli_close($con);
?>

正在生成

  

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\quiiz_portal\logedin.php on line 27

有人能告诉我这个PHP脚本有什么问题吗?

1 个答案:

答案 0 :(得分:3)

假设第27行如下:

$res=mysqli_query('SELECT COUNT(*) FROM qstns', $con);

你的参数倒退了。应该是:

$res = mysqli_query( $con, 'SELECT COUNT(*) FROM qstns' );

将数据返回$res后,您只需要解析它。

$row = mysqli_fetch_array($res);
echo $row[0];