Josh Blue
Josh Blue

Reputation: 9

mysqli bind_param fails or throws non-object error?

Hi I am converting over from normal way of doing things to this 'new' object way... Im not going to lie im quite confused and hope someone could help me here...

Below is my code

<?php
    include("common/functions.inc.php");
    $mysqli = new MySQLi($settings['mysql']['host'], $settings['mysql']['user'], $settings['mysql']['pass'], $settings['mysql']['db']);
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    if($stmt = $mysqli->prepare("INSERT INTO tbl_survey (fld_submituid, fld_q1answer, fld_q2answer, fld_q3answer, fld_q4answer, fld_q5answer, fld_q6answer) VALUES (:uid, :q1, :q2, :q3, :q4, :q5, :q6)")) {
        $stmt->bindParam(":uid",$uid, PDO::PARAM_INT);
        $stmt->bindParam(":q1",$q1, PDO::PARAM_STR, 12);    
        $stmt->bindParam(":q2",$q2, PDO::PARAM_STR, 12);
        $stmt->bindParam(":q3",$q3, PDO::PARAM_STR, 12);
        $stmt->bindParam(":q4",$q4, PDO::PARAM_STR, 12);
        $stmt->bindParam(":q5",$q5, PDO::PARAM_STR, 12);
        $stmt->bindParam(":q6",$q6, PDO::PARAM_STR, 12);
        $uid = 1;
        $q1 = "q1";
        $q2 = "q2";
        $q3 = "q3";
        $q4 = "q4";
        $q5 = "q5";
        $q6 = "q6";

        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        if($stmt->execute()) {
            echo 'true';
        } else {
            echo 'false';
        }
    } else {
        echo 'fail';
    }

I still do not why my code is failing, anyone help me please? Thank you

Upvotes: 0

Views: 540

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157910

mysqli doesn't support named placeholders.

Please read documentation and follow the proper syntax

To have an error message from prepare(), add this line before connect:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Upvotes: 3

Related Questions