Bind_param Non-Object Error w/ mysqli

拈花ヽ惹草 提交于 2019-11-27 08:08:56

问题


When attempting to insert the initial row for a table that will track daily views, I am getting the error:

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");

Any hints as to the problem?


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();
 }
}

回答1:


The problem was user error: I had the name of the result column wrong.

This was uncovered when I added echo $mysqli->error; after the line $stmt = $mysqli->prepare($sqlquery); which revealed the column-name error.



来源:https://stackoverflow.com/questions/3539523/bind-param-non-object-error-w-mysqli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!