Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

php - Bind_param Non-Object Error w/ mysqli

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();
 }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...