致命错误:在非对象上调用成员函数execute()

时间:2014-07-23 20:55:19

标签: php sql execute

$sql = "UPDATE Student ".
           "SET score = $total_score ".
           "WHERE student_id = $student_id";
    $stmt = $mysqli->prepare($sql);
    $stmt->execute();
    $query = "SELECT faculty_id ".
             "From Student s ".
             "WHERE student_id =$student_id";
    $state =$mysqli->prepare($query);
    $state->execute();
    $state->bind_result($faculty_id);
    if ($state->fetch())
    {if (strpos($faculty_id, '1') > 0) {
            include ('./Registration_Step_3_Student.php');
        } else 
        {
            include ('./Registration_Step_3_Mentor.php');
        }   
    }

因此,每当我尝试运行名为$ state的第二个查询时,我都会收到此错误,指出它无法执行。我是SQL和PHP的新手,所以任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

由于您使用的是mysqli,因此您应该了解如何正确绑定,请阅读bind_Param

mysqli_stmt::prepare在失败时返回false,你应该永远不执行该语句:

$sql = "UPDATE Student SET score = ? WHERE student_id = ?";
$stmt = $mysqli->prepare($sql);
if($stmt){
    $stmt->bind_param('si', $total_score, $student_id);
    if($stmt->execute()){
        $query = "SELECT faculty_id From `Student s` WHERE student_id = ?";
        $state =$mysqli->prepare($query);
        $stmt->bind_param('i', $student_id);
        if($state->execute()){
            var_dump($state->fetch());
        }else{
            echo 'SELECT failed';
             printf("Error: %s.\n", $state->error);
        }       
    }else{
       echo 'failed to execute UPDATE';
    }
}else{
      echo 'failed to prepare() UPDATE \n';
       printf("Error: %s.\n", $stmt->error);

}

希望这有帮助