1

In the following statement I am trying to fetch the result of the subjects table in the widget_crop database. But I am receiving the down error message after using the mysqli_multi_query() function in order to be able to use multiple mysql queries inside my php code . How can i correct this and print the table correctly ?

<?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);
?>

Error page

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 Answer 1

2

Replace your while loop code as below :

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());

check the URL

Sign up to request clarification or add additional context in comments.

1 Comment

each query returns the result for which we need to store in some variable and then that result is used as actual result for the each query separated by semicolon (;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.