准备好的声明不返回任何内容

发布于 2024-07-25 05:12:50 字数 1940 浏览 4 评论 0原文

我知道这个特定的查询是有效的,因为我用未经准备的过程方法测试了它。 就是这样:

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

我意识到,由于我有一个 id 列作为节表中的索引,因此我也需要将其绑定为结果,鉴于 php.net 上的上述语句(再次感谢 Bill)。

这是新代码:

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

再次感谢所有提供建议的人。 (我很好奇:我发现以这种方式使用 OOP 风格的准备好的语句时很难进行调试。例如,是否有一种简单的方法可以简单地查看实际使用的查询?)

如果我执行以下操作,就像一个简单的例子:

$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;
}

我的数据出现了,一切都很好。 然而,如果我做了以下事情:

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

我认为是正确的(当然,如果不正确,请随意大喊大叫),我什么也得不到。 更重要的是,使用该代码,当我进行 html 验证(以防万一)时,我收到内部服务器警告 (500),我认为这是 sql 代码的问题。 我只是疯了吗?

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

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?)

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. Am I just nuts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诺曦 2024-08-01 05:12:50

我没有看到您准备语句或使用参数有任何问题,但您的绑定结果有问题:

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

请注意,所有列都必须绑定
mysqli_stmt_execute()之后和之前
调用mysqli_stmt_fetch()

(强调我的)


上面的文档应该被视为查询中的所有列,而不是表中的所有列。

好吧,我刚刚自己尝试过。 如果我省略 $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

但它确实会获取数据。

如果我将 $name$content 都绑定到查询结果,则它可以正常工作,不会出现错误或警告。

所以我不得不问你:你确定数据库中有一行符合你的条件吗? 即name = 'introduction'在哪里? 请记住,在 SQL 中,字符串比较默认区分大小写。

我发现人们经常犯的一个错误是,他们在 PHP 脚本中连接到的数据库与用于即席查询的数据库不同。 因此,您需要绝对确定您正在验证数据是否存在于正确的数据库中。

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:

Note that all columns must be bound
after mysqli_stmt_execute() and prior
to calling 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:

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.

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'? Keep in mind that in SQL, string comparisons are case-sensitive by default.

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. So you need to be absolutely sure you're verifying that the data exists in the right database.

你对谁都笑 2024-08-01 05:12:50

这不应该是

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

当你选择 2 列时

Shouldn't that be

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

As you select 2 columns

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文