PHP mysqli_query至少需要2个参数

so my problem is with this code gives me the Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\forum\create_cat.php on line 13 if any1 knows what is there to be fixed here pls help


$sql = "SELECT
        cat_id,
        cat_name,
        cat_description,
    FROM
        categories";

$result = mysqli_query($sql);
if(!$result)
{
echo 'The categories could not be displayed, please try again later.';
}
else
}
if(mysqli_num_rows($result) == 0)
{
        echo 'No categories defined yet.';
}
else
{
 echo '<table border="1">
          <tr>
            <th>Category</th>
            <th>Last topic</th>
          </tr>'; 
  while($row = mysqli_fetch_assoc($result))
    {     
 echo '<tr>';
            echo '<td class="leftpart">';
                echo '<h3><a href="category.php?id">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'];
            echo '</td>';
            echo '<td class="rightpart">';
                        echo '<a href="topic.php?id=">Topic subject</a> at 10-10';
            echo '</td>';
        echo '</tr>';
  }
}

and i`m using this other file to connect to mydb

$server = 'localhost';
$username   = 'root';
$password   = '';
$database   = 'project yi';
 $link=mysqli_connect($server, $username,  $password, $database);

There are a few things wrong here.

Firstly, you need to pass db connection to your query.

What you are presently using:

$result = mysqli_query($sql);

What you need to use instead:

$result = mysqli_query($link, $sql);

Read the documention:

Procedural style

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

Then, you have a trailing comma in your query after cat_description

$sql = "SELECT
        cat_id,
        cat_name,
        cat_description, <<< right there
    FROM
        categories";
  • You need to remove it.

Checking for errors on your query would have spotted that.


Bonus answer to code you didn't post originally but left in a comment instead:

To answer what you posted above and not being part of your original question, is that your echo wrapping quotes are single quotes and you are also using single quotes inside there.

  • Just do echo "<p><form method='post' ...";

You're using single quotes for both the opening and closing statement and using single quotes for what is inside the echo statement.

Rewrite:

echo "<p><form method='post' action='create_cat.php'></p> 
Category name: <input type='text' name='cat_name' /> 
Category description: <textarea name='cat_description' /></textarea> 
<input type='submit' value='Add category' /> </form>";

Here is something that will work for what you're after. Hope this works :)

<?php 
        $Connection = mysql_connect('localhost', 'DBNAME', 'PASSWORD');
            if (!$Connection) {
                die('ACCESS DENIED: Cannot make a Secure Connection to the Database.<br><br>' . mysql_error());
        }

        $Database = mysql_select_db('main_webapp', $Connection);
            if (!$Database) {
                die ('DIED' . mysql_error());
        }

        $query = "SELECT * FROM table_name ORDER BY column_name ASC";
            $result = mysql_query( $query );
                if ( !$result ) {
                    $ErrorMessage  = 'Invalid query: ' . mysql_error() . "
";
                    $ErrorMessage .= 'Whole query: ' . $query;
                die( $ErrorMessage );
        }

        $JSON_output = array();
            while ( $row = mysql_fetch_assoc( $result ) )
        {

        $JSON_output[] = array('column_name'        => $row['column_name'], 
                                'column_name'       => $row['column_name'], 
                                'column_name'       => $row['column_name'], 
                                'column_name'       => $row['column_name']
                            );
        }

header( "Content-Type: application/json" );

    $JSON_output = json_encode($JSON_output);

echo $JSON_output;

mysql_close($Connection);
?>

Your output will be post via JSON to json.php

To call the code on the webpage, you will need to do

<script type="text/javascript">
                jQuery.ajax({
                     type: "GET",
                     url: "json.php",
                     dataType: "json",
                     success: function(response){
                        $.each(response, function(key, value){
                            var html = '' +
                                '<center><div class="col-md-3">'+value.column_name+'</div>'+
                                '<div class="col-md-3">'+value.column_name+'</div>'+
                                '<div class="col-md-3">'+value.column_name+'</div>'+
                                '<div class="col-md-3">'+value.column_name+'</div></center>';
                            $("#JSON_output").append(html);
                        });
                        }
                        });
                </script>