尝试为将跟踪每日视图的表插入初始行时,出现错误:
Fatal error: Call to a member function bind_param() on a non-object in /…/functions.php on line 157
该行是以下组的最后一行:
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");
关于这个问题有什么暗示吗?
以防万一周围的代码有问题,这是完整的功能:
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();
}
}
解决方法:
问题是用户错误:我的结果列名称错误.
当我添加echo $mysqli-> error时,发现了这一点;在$stmt = $mysqli-> prepare($sqlquery)行之后;揭示了列名错误.