准备好的声明不返回任何内容
我知道这个特定的查询是有效的,因为我用未经准备的过程方法测试了它。 就是这样:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有看到您准备语句或使用参数有任何问题,但您的绑定结果有问题:
http://php.net/manual/en/mysqli-stmt.bind-result.php 说:
(强调我的)
上面的文档应该被视为查询中的所有列,而不是表中的所有列。
好吧,我刚刚自己尝试过。 如果我省略
$name
列,它会给出以下警告:但它确实会获取数据。
如果我将
$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:
(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: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.
这不应该是
当你选择 2 列时
Shouldn't that be
As you select 2 columns