mysqli bind_param失败或抛出非对象错误?

时间:2013-10-23 11:22:36

标签: php mysql oop mysqli

嗨,我正在从正常的做事方式转变为这种“新的”对象方式......我不会撒谎我很困惑,希望有人能帮助我......

以下是我的代码

<?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';
    }

我仍然不知道为什么我的代码失败了,有人帮我吗?谢谢

1 个答案:

答案 0 :(得分:3)

mysqli不支持命名占位符。

请阅读documentation并遵循正确的语法

要收到prepare()的错误消息,请在连接前添加以下行:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);