$mysqli->prepare($query)
2番目のステートメントで動作しなくなるのはなぜですか?
$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
...
}
}
2番目のステートメントの繰り返しmysqli
接続を避けるには?prepare
更新: 私が見るように、コミュニティは実際の例を望んでいます:
データを含むdb テーブル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: