dqkkrt8860
dqkkrt8860
2018-11-20 15:27

绑定变量的数量与php和android中预准备语句中的字段数不匹配

已采纳

I tried running this script through Android, but the response is this;

Number of bind variables doesn't match number of fields in prepared statement

I do not know what this means, but my script was just supposed to log the person in if their input info is correct. Then, grab a string from the db and return it back to android. Can someone explain what im doing wrong? And what that error even means?

My script looks like this;

<?php
    $db_host = 'localhost:3306';
    $db_user = 'root';
    $db_pass = '';
    $db_name = 'test';

    $con = mysqli_connect($db_host,'user',$db_pass,$db_name);
    $username = $_POST["username"];
    $password = $_POST["password"];
    $statement = mysqli_prepare($con, "SELECT * FROM cresidentials WHERE username = ? AND password = ?");
    mysqli_stmt_bind_param($statement,"ss",$usermame,$password);
    mysqli_stmt_execute($statement);

    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement,$username,$password,$isAdmin);
    echo $isAdmin;
    ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongmi6102 dongmi6102 3年前

    OP's table of course also has an id column (which I discovered after he share a screenshot with me through chat), you also need to bind it.

    mysqli_stmt_bind_result($statement, $user_id, $username, $password, $isAdmin);

    Instead of:

    mysqli_stmt_bind_result($statement,$username,$password,$isAdmin);

    点赞 评论 复制链接分享
  • douqie6454 douqie6454 3年前

    Your table has four columns, but you're only binding three of them. Change this:

    mysqli_stmt_bind_result($statement,$username,$password,$isAdmin);
    

    To this:

    mysqli_stmt_bind_result($statement,$id,$username,$password,$isAdmin);
    

    Or change your select from this:

    SELECT * FROM ...
    

    To this:

    SELECT username, password, isAdmin FROM ...
    
    点赞 评论 复制链接分享