gpt4 book ai didi

php 的 mysqli_multi_query 不工作

转载 作者:行者123 更新时间:2023-11-29 21:24:31 26 4
gpt4 key购买 nike

我一直在尝试执行多个查询,因此我寻找了一种更好的方法来执行此操作,并且我在 php 中阅读了mysqli_multi_query

我自己尝试了一下看看结果,但它一直给我错误。代码如下:

$studid = $_GET['stud_id'];
$classcode = $_GET['class'];

$conn = new MySQLi($host, $username, $password, $dbname) or die('Can not connect to database');

$sql = "SELECT * FROM tbl_students WHERE stud_id = '".$studid."'";
$sql.= "SELECT * FROM tbl_classes WHERE class_code = '".$classcode."'";

if (mysqli_multi_query($conn, $sql)) {
do {
/* store first result set */
if ($result = mysqli_store_result($conn)) {
while ($row = mysqli_fetch_row($result)) {
$studname = $row[3].", ".$row[1];

}
mysqli_free_result($result);
}
/* print divider */
if (mysqli_more_results($conn)) {
printf("-----------------\n");
$studname = $row['fname'];
}
} while (mysqli_more_results($conn));
}else{ echo "error";}

$conn->close();

使用上面的代码,它只会打印我设置的 else 语句中的错误。我还尝试将第二个查询更改为 $sql .= "SELECT * FROM tbl_classes WHERE class_code = '".$classcode."'"; 并尝试在第一个查询后面加上分号来告诉 SQL我已经完成了第一个查询,因为我将 2 个字符串放在一起,但仍然没有运气。

最佳答案

试试这个

$studid = $_GET['stud_id'];
$classcode = $_GET['class'];

$conn = new MySQLi($host, $username, $password, $dbname) or die('Can not connect to database');

$sql = "SELECT * FROM tbl_students WHERE stud_id = '$studid';";
$sql.= "SELECT * FROM tbl_classes WHERE class_code = '$classcode'";

if ($conn->multi_query($sql)) {
do {
/* store first result set */
if ($result = mysqli_store_result($conn)) {
while ($row = mysqli_fetch_row($result)) {
$studname = $row[3].", ".$row[1];

}
mysqli_free_result($result);
}
/* print divider */
if (mysqli_more_results($conn)) {
printf("-----------------\n");
$studname = $row['fname'];
}
} while (mysqli_more_results($conn));
}else{ echo "error";}

$conn->close();

关于php 的 mysqli_multi_query 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35569427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com