我一直在讨论这个MySQL查询.
假设我有这个:
$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);
Run Code Online (Sandbox Code Playgroud)
有了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
Run Code Online (Sandbox Code Playgroud)
好吧,当它到达这一行时它会停止并且我收到此错误:
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个变量......我收到了这个错误.哎呀.
您的查询失败了prepare().你需要弄清楚在哪里,如何以及为什么.查看此答案的最后一个代码块,让我们知道错误是什么.
我将从查询开始.您正在尝试访问MySQL保留字源(请参阅#684).你需要用这样的反引号包装它们:
$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(?, ?, ?, ?, ?, ?, ?, ?, ?, ".
"?, ?, ?, ?, ?, ?, ?)";
Run Code Online (Sandbox Code Playgroud)
现在,您将$stmt在if块中实例化变量,然后尝试将其绑定到该块之外.你需要改变这个:
if ($stmt = $mysqli->prepare($add)) {
....
}
$stmt->bind_param(....);
Run Code Online (Sandbox Code Playgroud)
对此:
if ($stmt = $mysqli->prepare($add)) {
....
$stmt->bind_param(....);
}
Run Code Online (Sandbox Code Playgroud)
此外,请确保您的查询实际上正在准备:
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);
}
Run Code Online (Sandbox Code Playgroud)
然后告诉我们结果如何.
| 归档时间: |
|
| 查看次数: |
16989 次 |
| 最近记录: |