带mysqli的Bind_param非对象错误 [英] Bind_param Non-Object Error w/ mysqli

查看:47
本文介绍了带mysqli的Bind_param非对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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(). strong>

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

推荐答案

问题是用户错误:我在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非对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆