問題描述
嘗試為將跟蹤每日視圖的表插入初始行時,出現錯誤:
When attempting to insert the initial row for a table that will track daily views, I am getting the error:
致命錯誤:在 157 行/.../functions.php 中的非對象上調用成員函數 bind_param()強>
Fatal error: Call to a member function bind_param() on a non-object in /.../functions.php on line 157
該行是以下組的最后一行:
That line is the last of the following group:
if($stats_found) {
$sqlquery = "UPDATE vid_stats SET views = ? WHERE title = ? AND format = ? AND date = ? AND results = ?";
$views++;
} else {
$sqlquery = "INSERT INTO vid_stats (views, title, format, results) values (?, ?, ?, ? )";
$views = 1;
}
$stmt = $mysqli->prepare($sqlquery);
/* bind parameters for markers */
$stmt->bind_param("dsss", $views, $title, $format, "success");
關于問題的任何提示?
以防萬一這是周圍代碼的問題,這里是完整的功能:
Just in case it's an issue with the surrounding code, here is the complete function:
function updateViewCount($title, $format, $results) {
//update view count
global $mysqli;
$views = 0;
if ($stmt = $mysqli->prepare("SELECT views FROM vid_stats WHERE title = ? AND format = ? AND date = ?")) {
/* bind parameters for markers */
$stmt->bind_param("ssd", $title, $format, date("Y-m-d"));
/* execute query */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($views);
/* fetch value */
if ($stmt->fetch()) {
$stats_found = true;
} else { $stats_found = false; }
/* close statement */
$stmt->close();
if($stats_found) {
$sqlquery = "UPDATE vid_stats SET views = ? WHERE title = ? AND format = ? AND date = ? AND results = ?";
$views++;
} else {
$sqlquery = "INSERT INTO vid_stats (views, title, format, results) values (?, ?, ?, ? )";
$views = 1;
}
$stmt = $mysqli->prepare($sqlquery);
/* bind parameters for markers */
echo $sqlquery."<br>".$views."<br>".$title."<br>".$format;
$stmt->bind_param("dsss", $views, $title, $format, "success");
/* execute query */
$stmt->execute();
/* close statement */
$stmt->close();
}
}
推薦答案
問題是用戶錯誤:我把 result
列的名稱弄錯了.
The problem was user error: I had the name of the result
column wrong.
當我在行 $stmt = $mysqli->prepare($sqlquery);
之后添加 echo $mysqli->error;
時發現了這一點,它顯示列名錯誤.
This was uncovered when I added echo $mysqli->error;
after the line $stmt = $mysqli->prepare($sqlquery);
which revealed the column-name error.
這篇關于帶有 mysqli 的 Bind_param 非對象錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!