PHP-致命错误:调用成员函数bind_param() [英] PHP - Fatal error: Call to a member function bind_param()

查看:117
本文介绍了PHP-致命错误:调用成员函数bind_param()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决这个MySQL查询.

I've been pulling my hair out on this MySQL query.

比方说我有这个:

    $add = "INSERT INTO books (title) VALUES(?)";
    if ($stmt = $mysqli->prepare($add)) {

        $arr = array($title);

        foreach ($arr as $value) {
            echo var_dump($value);
        }

        $stmt->bind_param("s", $title);

使用该foreach-> var_dump:

string 'Medieval Times (History)' (length=24)
int 1422843281
int 1420844341
string '127.0.0.1' (length=9)
string 'MY_EMAIL@gmail.com' (length=22)
string '' (length=0)
int 1420844805
int 6
int 3
int 1
int 0
int 0
int 1
int 1
int 1
int 1

好吧,当它碰到这一行时它停止了,我得到了这个错误:

Well, it stops when it hits this line and I get this error:

Fatal error: Call to a member function bind_param() on a non-object in C:\wamp\www\books\dashboard.php on line 386

第386行:$stmt->bind_param ...

所以,我知道我要导入16个变量……我收到此错误.啊.

So, I know I am importing 16 variables yet ... I get this error. Argh.

推荐答案

TL \ DR

您的查询无法执行prepare().您需要弄清楚位置,方式和原因.查看此答案的最后一个代码块,然后让我们知道错误是什么.

TL\DR

Your query is failing to prepare(). You need to figure out where, how and why. Look at the last code block of this answer and let us know what the error is.

我将从查询开始.您正在尝试访问MySQL保留字 来源(请参阅#684) .您需要像这样将它们包装在反引号中:

I'll start with the query. You're trying to access a MySQL reserved word Source (See #684). You need to wrap those in backticks like this:

$add = "INSERT INTO books (title, edited, created, ip,".
    " email_to, twitter, last_taken, questions_total, responses, ".
    "show_progress, need_correct, go_back, state, send_stats, ".
    "show_number, imported) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ".
    "?, ?, ?, ?, ?, ?, ?)";

现在,您要在if块中实例化变量$stmt,然后尝试将其绑定到该块之外.您需要对此进行更改:

Now, you're instantiating the variable $stmt within the if block but then trying to bind it outside of that block. You'll need to change this:

if ($stmt = $mysqli->prepare($add)) {
....
}
$stmt->bind_param(....);

对此:

if ($stmt = $mysqli->prepare($add)) {
....
$stmt->bind_param(....);
}

此外,请确保您的查询实际上正在正确准备:

Also, make sure your query is actually preparing correctly:

if ($stmt = $mysqli->prepare($add)) {

    $stmt->bind_param("siisssiiiiiiiiii", $title, $edited, $created, $ip, $email_to, $twitter, $last_taken, $questions_total, $responses, $show_progress, $need_correct, $go_back, $state, $send_stats, $show_number, $importedVal);

    // execute it and all...
} else {
    die("Errormessage: ". $mysqli->error);
}

然后让我们知道发生了什么.

Then let us know what turns up.

这篇关于PHP-致命错误:调用成员函数bind_param()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆