如何实现MySQLi嵌套的准备好的语句? [英] How to implement MySQLi nested prepared statements?
问题描述
我正在尝试将MySQL转换为MySQLi.而且我不知道为什么会刹车
I am trying to convert MySQL to MySQLi. And I cannot figure out why it brakes on
$stmt2->execute();
并返回错误:
Call to a member function execute() on a non-object
任何问题或有效实施!!
Any issue or valid implementing of it!?
// SQL condition "WHERE group=''" where `group` is empty (NULL)
$result = "SELECT id, name FROM table WHERE group='' ORDER BY array ASC";
if ($stmt = $mysqli->prepare($result)) {
$stmt->execute();
$stmt->bind_result($id, $name);
while ($stmt->fetch()) {
// SQL condition "WHERE group='$id'" where $id defined in $stmt->bind_result($id, $name);
$result2 = "SELECT name FROM table WHERE group='$id' ORDER BY array ASC";
$stmt2 = $mysqli->prepare($result2);
//$valid_stmt2 = $stmt2 === FALSE ? false : true;
echo $name . "\n";
//if ($valid_stmt2) {
// Error cased on $stmt2->execute();
$stmt2->execute();
$stmt2->bind_result($name2);
while ($stmt2->fetch()) {
echo 'related to: ' . $name2 . "\n";
}
$stmt2->close();
//}
}
$stmt->free_result();
$stmt->close();
}
此问题可能与可以使用多个/嵌套的MySQLi语句有关? 不幸的是,由于它没有提供有效的示例或问题资源,因此我认为它没有帮助.
This question might be related to Possible to use multiple/nested MySQLi statements? Unfortunately I did not find it helpful since it does not provide a valid example or resource for issue.
更新:带有注释的简化代码示例.
推荐答案
您先做
"SELECT id, url, name FROM links WHERE group='' ORDER BY array ASC"
然后您要使用$id
进行操作
"SELECT url, name FROM links WHERE group='$id' ORDER BY array ASC"
如何得到结果?如果组等于空字符串,那么它将不等于$id
(仅当$id
也为空字符串,但这是没有意义的.)
How would get result for that? If group equal empty string, then it will not equal $id
(only if $id
is empty string too but that's non sense.)
这篇关于如何实现MySQLi嵌套的准备好的语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!