为什么它停止在第二个$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: