gpt4 book ai didi

php - 在 mysqli_multiple_query() 函数之后获取数组

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

在下面的语句中,我试图获取 widget_crop 数据库中 subjects 表的结果。但是在使用 mysqli_multi_query() 函数以便能够在我的 php 代码中使用多个 mysql 查询后,我收到了错误消息。我怎样才能更正这个问题并正确打印表格?

<?php
//1.Create a database connection
$dbhost = "localhost";
$dbuser ="widget_cms";
$dbpass="12605532y";
$dbname = "widget_crop";
$connection = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);

//test if connection occured
if(mysqli_connect_errno()){
die("Database connection failed !".
mysqli_connect_error().")".mysqli_connect_errno().")"
);
}
?>
<?php
$menu_name = "\"Edit me\"";
$position=4;
$visible = 1;
// Often these are form values in POST
// 2.Perform a db query
$query ="INSERT INTO ";
$query .="subjects (menu_name,position,visible) ";
$query .="VALUES($menu_name,$position,$visible);\r\n";
$query .="SELECT *";
$query .="FROM subjects ";
$query .="WHERE visible = 1 ";
$query .="ORDER BY position ASC;";
echo $query;
$result = mysqli_multi_query($connection,$query);
if(!$result){
die("Database query failed !");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<style>
body{
font-family:tahoma;
font-size:0.75em;
}
table{
border-collapse: collapse;
}
td{
padding-top:5px;
padding-bottom:5px;
padding-right:15px;
padding-left:15px;
background: lightblue;
border-color: white;
}

</style>

<html lang="en">
<head>
<title>untitled</title>
</head>
<body>
<table border="2">
<?php
while($row = mysqli_fetch_assoc($result)){
// 3. Use returned Data
// output date from each row
echo "<tr>";
echo"<td>". $row["id"] ."</td>";
echo"<td>". $row["menu_name"] ."</td>";
echo"<td>". $row["position"] ."</td>";
echo"<td>". $row["visible" ]."</td>";
echo "</tr>";
}
?>
</table>
<?php
// 4. release the returned data
mysqli_free_result($result);
?>
</body>
</html>
<?php
// 5. Close the connection
mysqli_close($connection);
?>

错误页面

SELECT *FROM subjects WHERE visible = 1 ORDER BY position ASC;<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<style>
body{
font-family:tahoma;
font-size:0.75em;
}
table{
border-collapse: collapse;
}
td{
padding-top:5px;
padding-bottom:5px;
padding-right:15px;
padding-left:15px;
background: lightblue;
border-color: white;
}

</style>

<html lang="en">
<head>
<title>untitled</title>
</head>
<body>
<table border="2">
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\lab\databases_insert.php on line <i>66</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>248640</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\lab\databases_insert.php' bgcolor='#eeeeec'>..\databases_insert.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0130</td><td bgcolor='#eeeeec' align='right'>260440</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysqli-fetch-assoc' target='_new'>mysqli_fetch_assoc</a>
( )</td><td title='C:\wamp\www\lab\databases_insert.php' bgcolor='#eeeeec'>..\databases_insert.php<b>:</b>66</td></tr>
</table></font>
</table>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\lab\databases_insert.php on line <i>80</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>248640</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\lab\databases_insert.php' bgcolor='#eeeeec'>..\databases_insert.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0450</td><td bgcolor='#eeeeec' align='right'>261072</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysqli-free-result' target='_new'>mysqli_free_result</a>
( )</td><td title='C:\wamp\www\lab\databases_insert.php' bgcolor='#eeeeec'>..\databases_insert.php<b>:</b>80</td></tr>
</table></font>
</body>
</html>

最佳答案

替换你的 while 循环代码如下:

do {
/* store first result set */
if ($result = mysqli_store_result()) {
while ($row = $result->fetch_row()) {
if(!empty($row["id"])){
echo "<tr>";
echo"<td>". $row["id"] ."</td>";
echo"<td>". $row["menu_name"] ."</td>";
echo"<td>". $row["position"] ."</td>";
echo"<td>". $row["visible" ]."</td>";
echo "</tr>";
}
}
$result->free();
}
/* print divider */
if (mysqli_more_results()) {
printf("-----------------\n");
}
} while (mysqli_next_result());

检查URL

关于php - 在 mysqli_multiple_query() 函数之后获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32332430/

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