如何在绑定结果中删除此mysqli警告?

时间:2012-08-10 13:57:08

标签: php mysql database mysqli

我在错误报告中收到mysqli警告,说明Warning: mysqli_stmt::bind_result() [mysqli-stmt.bind-result]: Number of bind variables doesn't match number of fields in prepared statement in ... on line 36有没有人知道如何正确绑定结果以便此警告可以消失?我不能真正看出问题是什么,但后来说这是我在使用mysqli时的初学者。

以下是代码:

<?php
  // PHP code
  session_start(); 

//connected to db

  // required variables (make them explciit no need for foreach loop)
  $teacherusername = (isset($_POST['teacherusername'])) ? $_POST['teacherusername'] : '';
  $teacherpassword = (isset($_POST['teacherpassword'])) ? $_POST['teacherpassword'] : '';
  $loggedIn = false;

  if (isset($_POST['submit'])) {

    $teacherpassword = md5(md5("j3Jf92".$teacherpassword."D203djS"));  

    // don't use $mysqli->prepare here
    $query = "SELECT * FROM Teacher WHERE TeacherUsername = ? AND TeacherPassword = ? LIMIT 1";
    // prepare query
    $stmt=$mysqli->prepare($query);
    // You only need to call bind_param once
    $stmt->bind_param("ss",$teacherusername,$teacherpassword);
    // execute query
    $stmt->execute(); 
    // get result and assign variables (prefix with db)
    $stmt->bind_result($dbTeacherForename,$dbTeacherSurname,$dbTeacherUsername,$dbTeacherPassword);

    while($stmt->fetch()) {
      if ($teacherusername == $dbTeacherUsername && $teacherpassword == $dbTeacherPassword) {
        $loggedIn = true;
      }
    }

    if ($loggedIn == true){
      // left your session code as is - but think wisely about using
      $_SESSION['teacherforename'] = $dbTeacherForename;
      $_SESSION['teachersurname'] = $dbTeacherSurname;
      header( 'Location: menu.php' ) ;
      die();
    }

       /* close statement */
    $stmt->close();

    /* close connection */
    $mysqli->close();
  }
?>

2 个答案:

答案 0 :(得分:1)

不推荐使用通配符*。可能表中的列数多于您需要的4列?

我会选择

SELECT TeacherForname, TeacherSurname, TeacherUsername, TeacherPassword FROM Teacher WHERE TeacherUsername = ? AND TeacherPassword = ? LIMIT 1

答案 1 :(得分:0)

发生错误是因为您要求mysqli将x列绑定到y x != y变量Teacher。 {{1}}表中有多少列?