带有 mysqli 的 Bind_param 非对象错误

2021-12-25 00:00:00 php mysql mysqli sqlbindparameter

尝试为将跟踪每日视图的表插入初始行时,出现错误:

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.

相关文章