如何实现的MySQLi嵌套预处理语句? [重复](How to implement MySQLi

2019-07-29 23:11发布

我想MySQL的转换为MySQLi的。 我想不通为什么它刹车

$stmt2->execute();

并返回错误:

Call to a member function execute() on a non-object

任何问题或有效实施的吧!?

// 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的语句? 不幸的是我没有发现它有用,因为它没有提供有效的例子或资源的问题。

更新:有评论简化代码示例。

Answer 1:

你第一次做

"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 to implement MySQLi nested prepared statements? [duplicate]