Call to a member function bind_param() on boolean

2020-01-29 21:22发布

问题:

I try to bind parameters to sql (mysqli) in php, but there is an error code as written above. This is the code I wrote:

$stmt = $conn->prepare("INSERT INTO assignments (Classes_has_Subjects_classesHasSubjectsId, assignmentName, gradeForAssignment,protectiveGrade) 
                                  VALUES (?, ?, ?, ?)");
if ($stmt = false)
{
    echo "false";
}
else{
    $stmt->bind_param("sss", $classesHasSubjectsId, $assignmentName, $gradeForAssignment,  $protectiveGrade);
    $stmt->execute();
}

and here is the error message: 11

Fatal error: Call to a member function bind_param() on boolean in

What's wrong?

回答1:

<?php
$stmt = $conn->prepare("INSERT INTO assignments (Classes_has_Subjects_classesHasSubjectsId, assignmentName, gradeForAssignment,protectiveGrade) 
                                  VALUES (?, ?, ?, ?)");
    if (!$stmt) {
        echo "false";
    }
    else {
        $stmt->bind_param("ssss", $classesHasSubjectsId, $assignmentName, $gradeForAssignment,  $protectiveGrade);
        $stmt->execute();
    }

if ($stmt = false) means you're assigning false to $stmt. Replace it with if (!$stmt).

You're binding 4 parameters but only providing 3 types of values. I added a s in the bind_param function, assuming all your values are string. If some of the values are integers replace the corresponding s with an i. If there are doubles, replace with a d.

Are you sure the first field's name in the assignments table is Classes_has_Subjects_classesHasSubjectsId?



标签: php mysqli