简体   繁体   English

准备好的声明不返回任何东西

[英]Prepared Statement not returning anything

I know this particular query works, as I tested it with unprepared, procedural methods.我知道这个特定的查询是有效的,因为我用未经准备的程序方法对其进行了测试。 Here it is:这里是:

$name = 'introduction';
$mysqli = new mysqli('localhost', 'user', 'pass', 'db') or die('There was a problem connecting to the database.');
$stmt = $mysqli->prepare("SELECT name, content FROM sections WHERE name = ?");
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->bind_result($content);
$stmt->fetch();
echo  $content;
$stmt->close();

I realized that, since I have an id column as an index in the sections table, I needed to bind that as a result as well, given the above statement at php.net, (thanks again, Bill).我意识到,由于我在部分表中有一个 id 列作为索引,因此鉴于 php.net 上的上述语句,我也需要绑定它(再次感谢,比尔)。

Here's the new code:这是新代码:

$name = 'introduction';
$mysqli = new mysqli('localhost', 'user', 'pass', 'db') or die('There was a problem connecting to the database.');
$stmt = $mysqli->prepare("SELECT name, content FROM sections WHERE name = ?");
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->bind_result($id, $name, $content);
$stmt->fetch();
echo $content;
$stmt->close();

Thanks again to all who can offer suggestions.再次感谢所有可以提供建议的人。 (I'm curious: I find it hard to debug when using the OOP style of prepared statements in this way. Is there, for example, an easy way to simply see the query that was actually used?) (我很好奇:我发现以这种方式使用准备好的语句的 OOP 样式时很难调试。例如,有没有一种简单的方法可以简单地查看实际使用的查询?)

If I do the following, just as a quick-and-dirty example:如果我执行以下操作,就像一个快速而肮脏的例子:

$name = 'introduction';
@mysql_connect('host', 'user', 'pass');
@mysql_select_db('db');
$query = "SELECT name,content FROM sections WHERE name = '$name'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_object($result)) {
    $content = $row->content;
    echo $content;
}

My data appears and all is well.我的数据出现了,一切都很好。 If, however, I do the following:但是,如果我执行以下操作:

$name = 'introduction';
$mysqli = new mysqli('localhost', 'user', 'pass', 'db') or die('There was a problem connecting to the database.');
$stmt = $mysqli->prepare("SELECT name, content FROM sections WHERE name = ?");
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->bind_result($name, $content);
$stmt->fetch();
echo  $content;
$stmt->close();

Which I believe is correct (feel free to yell if not, of course), I get nothing.我认为这是正确的(当然,如果不正确,请随意大喊),我什么也没得到。 What's more, with that code, when I do an html validation (just in case), I get an internal server warning (500), which I take to be a problem with the sql code.更重要的是,使用该代码,当我进行 html 验证(以防万一)时,我收到内部服务器警告 (500),我认为这是 sql 代码的问题。 Am I just nuts?我只是疯了吗?

I don't see anything wrong with your preparation of the statement or use of parameters, but there is something wrong in your binding results:我看不出你准备语句或使用参数有什么问题,但你的绑定结果有问题:

http://php.net/manual/en/mysqli-stmt.bind-result.php says: http://php.net/manual/en/mysqli-stmt.bind-result.php说:

Note that all columns must be bound after mysqli_stmt_execute() and prior to calling mysqli_stmt_fetch() .请注意,必须在mysqli_stmt_execute()和调用mysqli_stmt_fetch()之前绑定所有列

(emphasis mine) (强调我的)


The above doc should be taken as all columns in your query, not all columns in your table.应将上述文档视为查询中的所有列,而不是表中的所有列。

Okay, I just tried this myself.好的,我只是自己试过这个。 If I omit the $name column, it gives this warning:如果我省略$name列,它会给出以下警告:

PHP Warning:  mysqli_stmt::bind_result(): Number of bind variables doesn't 
match number of fields in prepared statement in mysqli.php on line 9
PHP Stack trace:
PHP   1. {main}() /Users/bill/workspace/PHP/mysqli.php:0
PHP   2. mysqli_stmt->bind_result() /Users/bill/workspace/PHP/mysqli.php:9

But it does fetch the data.但它确实获取了数据。

If I bind both $name and $content to the results of the query, it works without error or warning.如果我将$name$content都绑定到查询的结果,它就可以正常工作而不会出现错误或警告。

So I'm forced to ask you: are you sure there's a row in the database that matches your condition?所以我不得不问你:你确定数据库中有一行符合你的条件吗? That is, where name = 'introduction' ?也就是说,在哪里name = 'introduction' Keep in mind that in SQL, string comparisons are case-sensitive by default.请记住,在 SQL 中,字符串比较默认区分大小写。

One mistake I see people make frequently is that they connect to a different database in their PHP script than the database they use for ad hoc queries.我看到人们经常犯的一个错误是,他们在 PHP 脚本中连接到的数据库与用于临时查询的数据库不同。 So you need to be absolutely sure you're verifying that the data exists in the right database.因此,您需要绝对确定您正在验证数据是否存在于正确的数据库中。

Shouldn't that be不应该是

$stmt->bind_result($name, $content);

As you select 2 columns当您选择 2 列时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM