編集済み:以下の作業コード!
//* opens a connection to a MySQL server */
$mysqli = new mysqli($host, $username, $password, $database);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "INSERT INTO `test` (test1, test2) VALUES('testing','testing2222');";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing3333','testing44444');";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing5555','testing66666');";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing77777','testing888888');";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing99999','testing101010');";
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
//printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
//if ($mysqli->more_results()) {
// printf("-----------------\n");
//}
} while ($mysqli->next_result());
}
/* close connection */
$mysqli->close();
以下の悪いコード:
私はこれを可能な限り試しましたが、うまくいかないようです。入力する $query が 1 つしかない場合にのみ、1 つのバリエーションが機能することに気付きました。複数ある場合は、失敗します。
//* opens a connection to a MySQL server */
$mysqli = mysql_connect($host,$username,$password, $database);
$query = "INSERT INTO `test` (test1, test2) VALUES('testing','testing2222')";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing3333','testing44444')";
if($mysqli->multi_query($query))
{
do
{
if($result=$mysqli->store_result())
{
while($row=$result->fetch_row())
{
printf("%s<br/>",$row[0]);
}
$result->free();
}
if($mysqli->more_results())
{
print("-------------------------------<br/>");
}
else
{
echo '<br/>';
}
}while($mysqli->more_results() && $mysqli->next_result());
}
接続を作成します。簡単に理解できるように、コードの一部をトリミングしました。テーブルが存在するなど...メインの例も一言一句試してみましたが、うまくいかないようです。どこが間違っているのですか?