0

为什么它停止在第二个$mysqli->prepare($query)语句上工作?

$mysqli = @new mysqli(HOSTNAME, USERNAME, PASSWORD, DATABASE);
...
if ($stmt = $mysqli->prepare($query)) {
    // Code is working fine
    ...
    if ($stmt2 = $mysqli->prepare($query2)) {
        // Code does not work
        ...
    }
}

并且可以通过重复mysqli连接正常工作:

$mysqli = @new mysqli(HOSTNAME, USERNAME, PASSWORD, DATABASE);
$mysqli2 = @new mysqli(HOSTNAME, USERNAME, PASSWORD, DATABASE);
...
if ($stmt = $mysqli->prepare($query)) {
    // Code is working fine
    ...
    if ($stmt2 = $mysqli2->prepare($query2)) {
        // Code is working fine
        ...
    }
}

如何避免第二条语句mysqli的重复连接?prepare


更新:正如我所见,社区想要一个真实的例子:

fruits带有数据的db 表:

CREATE TABLE IF NOT EXISTS `fruits` (
  `id` varchar(8) NOT NULL,
  `group` varchar(8) NOT NULL,
  `name` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `fruits` (`id`, `group`, `name`) VALUES
('03E7', '', 'Berries'),
('0618', '03E7', 'blueberry'),
('051B', '03E7', 'raspberry'),
('02AA', '03E7', 'strawberry'),
('035F', '', 'Citrus'),
('07A5', '035F', 'grapefruit'),
('0633', '035F', 'lime'),
('05E1', '', 'Pear');

php脚本:

<?php

$mysqli = new mysqli('localhost', 'root', 'password', 'test');
//$mysqli2 = new mysqli('localhost', 'root', 'password', 'test');

$query1 = "SELECT id, name FROM fruits WHERE `group`=''";
$query2 = "SELECT name FROM fruits WHERE `group`=?";

$stmt1 = $mysqli->stmt_init();
$stmt2 = $mysqli->stmt_init();
//$stmt2 = $mysqli2->stmt_init();

if($stmt1->prepare($query1)){
    $stmt1->execute();
    $stmt1->bind_result($id, $name1);

    while($stmt1->fetch()){
        echo $name1;

        if($stmt2->prepare($query2)){
            $stmt2->bind_param('s', $group);
            $group = $id;
            $stmt2->execute();
            $stmt2->bind_result($name2);

            echo ':';

            while($stmt2->fetch()){
                echo ' ' . $name2 . ',';
            }
        }

        echo '<br>';
    }
}

?>

结果:

Berries
Citrus
Pear

预期结果:

Berries: blueberry, raspberry, strawberry,
Citrus: grapefruit, lime,
Pear:
4

2 回答 2

1

要解决此问题,请删除

$stmt1 = $mysqli->stmt_init();
$stmt2 = $mysqli->stmt_init();

并在之后添加$stmt1->execute();

$stmt1->store_result();

$stmt1->prepare($query1)并将,替换$stmt2->prepare($query1)

$stmt1 = $mysqli->prepare($query1)
$stmt2 = $mysqli->prepare($query2)
于 2012-07-10T21:32:03.353 回答
-1

您的代码是否有以下内容?

$stmt = $mysqli->stmt_init();
$stmt2 = $mysqli->stmt_init();

然后你可以这样做:

if($stmt->prepare($query)) ...
if($stmt2->prepare($query2)) ...

请发布所有代码,以便我可以更好地了解发生了什么。

编辑:好的,这是我的工作示例:

这是我links的数据表:

CREATE TABLE IF NOT EXISTS `links` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(32),
  `group` varchar(32),
  `array` int(32),
  PRIMARY KEY (`id`)
) ENGINE=MyISAM;

INSERT INTO `links` (`id`, `name`, `group`, `array`) VALUES
(1, 'link1', '', 1),
(2, 'link1-sub-link1', '1', 1),
(3, 'link1-sub-link2', '1', 1),
(4, 'link2', '', 2),
(5, 'link3', '', 2),
(6, 'link3-sub-link1', '5', 2),
(7, 'link3-sub-link2', '5', 3),
(8, 'link3-sub-link3', '5', 3);

以下 php 代码对我来说执行没有错误:

<?php
$mysqli = new mysqli("localhost", "root", "password", "things");

$query = "SELECT id, name FROM links WHERE `group`='' ORDER BY array ASC";
$query2 = "SELECT name FROM links WHERE `group`=? ORDER BY array ASC";

$stmt1 = $mysqli->stmt_init();
$stmt2 = $mysqli->stmt_init();
if($stmt1->prepare($query)){
    $stmt1->execute();
    $stmt1->bind_result($id, $name1);
    while($stmt->fetch()){
    echo $name1;
        if($stmt2->prepare($query2)){
            $stmt2->bind_param('s', $group);
            $group = $id;
            $stmt2->execute();
            $stmt2->bind_result($name2);
            while($stmt2->fetch()){
                echo 'related to: ' . $name2 . "\n";
            }
        }
    }
}
?>
于 2012-06-18T14:15:34.837 回答