在mysqli_multiple_query()函数

时间:2015-09-01 13:03:07

标签: php mysql

在下面的语句中,我试图在widget_crop数据库中获取subject表的结果。但是我在使用mysqli_multi_query()函数后收到down错误消息,以便能够在我的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>

1 个答案:

答案 0 :(得分:2)

替换你的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