如何避免重复MySQLi连接第二个`prepare`语句?

时间:2012-06-17 20:40:36

标签: mysqli prepared-statement

为什么它停止在第二个$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次连接?


更新:我认为社区需要一个真实的例子:

db table fruits包含数据:

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:

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:-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";
            }
        }
    }
}
?>