MYSQLI准备好的语句无输出 [英] MYSQLI prepared statement proceeds no output

查看:54
本文介绍了MYSQLI准备好的语句无输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习MYSQLI准备的语句技术,并且遇到了非常烦人的问题.我的代码在bind_result()处停止.而且我只是不明白可能出什么问题了,所以,伙计们,请帮帮我.

I've just started learning MYSQLI prepared statements technique and faced really annoying issue. My code stops at bind_result(). And I just don't get it what could be wrong, so please, guys, help me.

这是我的config.php:

Here is my config.php:

 <?php 
  $db_name = 'site'; 
  $db_user = 'root'; 
  $db_pass = 'root'; 
  $db_host = 'localhost';
  $db_port = 8889;

  $db = new mysqli($db_host, $db_user, $db_pass, $db_name, $db_port);

  if($db->connect_errno > 0){
  die('Unable to connect to database [' . $db->connect_error . ']');
  }
 ?>

还有我的主要代码:

 <?php
  include 'config.php';
  $post_id=450;
  $stmt = $db->prepare("SELECT * FROM messages WHERE post_id = ?");
  if ( false===$stmt ) {
  die('prepare() failed: ' . htmlspecialchars($db->error));
  }
  $ex=$stmt->bind_param('i', $post_id); // Bind "$post_id" to parameter.
  if ( false===$ex ) {
  die('bind_param() failed: ' . htmlspecialchars($stmt->error));
  }
  $ex=$stmt->execute();// Execute the prepared query.
  if ( false===$ex ) {
  die('execute() failed: ' . htmlspecialchars($stmt->error));
  }
  $ex=$stmt->store_result();
  if ( false===$ex ) {
  die('store_result() failed: ' . htmlspecialchars($stmt->error));
  } 
  $ex=$stmt->bind_result($p); 
  if ( false===$ex ) {
  die('bind_result() failed: ' . htmlspecialchars($stmt->error));
  }
  while($stmt->fetch()){
  echo $p;
  }

 ?>

推荐答案

您最初的问题是您没有检查每个调用是否失败,只是在开始时检查了几次. store_resultbind_result也会失败.

Your initial problem is that you weren't checking every call for failure, just the few at the start. Both store_result and bind_result can also fail.

您的特定问题(一旦您解决了最初的问题)是您正在执行select *以获取多个列,但是您只绑定了一个结果.从bind_result调用得​​到的错误消息中可以明显看出这一点:

Your specific problem (once you've fixed the initial one) is that you're doing select * to get multiple columns but you're only binding one in the result. That's evident from the error message you get from the bind_result call:

绑定变量数与字段数不匹配

Number of bind variables doesn't match number of fields

如果将select语句更改为类似select id from ...的内容,则会发现它开始起作用.

If you change the select statement to something like select id from ..., you'll find it starts working.

但是,假设您要想要更多列,则应在绑定中提供更多变量,例如:

However, assuming you want more columns, you should provide more variables in the binding, something like:

$stmt = $db->prepare("SELECT col1, col2 FROM messages WHERE post_id = ?");
:
$ex = $stmt->bind_result($param1, $param2);

您会发现我不在那儿使用select *,最好在几乎所有情况下都使用显式列命名,以便:

You'll notice I'm not using select * up there, it's better to use explicit column naming in almost all situations so that:

  • 当数据库架构更改时,您不会感到惊讶;
  • 您仅获得所需的数据;
  • 您可以将列正确映射到结果变量.

我曾经发现可以使用select *的几乎是 only 的地方,它在DB分析工具中,您想获得所有东西.如果您事先知道想要什么,那是您应该要求的.

Pretty much the only place I've ever found it's okay to use select * is in DB analysis tools where you want to get everything. If you know what you want in advance, that's what you should ask for.

这篇关于MYSQLI准备好的语句无输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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