-3

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);
Josip Ivic
  • 3,639
  • 9
  • 39
  • 57
DLMike
  • 35
  • 6

2 Answers2

3

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>";
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • this helped ..i got over the error ..now i`m struggling with posting the form itself for my category Db ..and i get this error ---Parse error: syntax error, unexpected 'post' (T_STRING), expecting ',' or ';' in C:\wamp\www\forum\create_cat.php on line 6 -----using --- echo '

    Category name: Category description:
    ';
    – DLMike Sep 20 '15 at 09:06
  • @DLMike I believe I answered the original question. You will need to mark this one as solved. That error you got isn't part of your original posted code. You will need to post a new question with the code you are using that you did not show us originally. – Funk Forty Niner Sep 20 '15 at 13:30
  • @DLMike 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 "

    – Funk Forty Niner Sep 20 '15 at 13:43
-1

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() . "\n";
                    $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>
Levi
  • 81
  • 13
  • Let me know if this worked for what you're trying to do. – Levi Sep 20 '15 at 00:46
  • You know that mysql functions is deprecated and is very vulnerable to attacks. The user already used mysqli – wobsoriano Sep 20 '15 at 01:43
  • i`m new to php ..and i`m trying to make php forum for my diploma project ...and i just have one question ...what is supposed to be completed here +value.column_name+ ......column name is for me cat_id....and so on ..but what should i enter instead +value ....? – DLMike Sep 20 '15 at 09:05
  • you want to change the +value.column_name+ to +value.cat_id+ – Levi Sep 20 '15 at 21:49
  • @FewFlyBy regardless you shouldnt come in and down vote my post... I would appreciate it if you changed it from a down to a neutral, or a up... – Levi Sep 20 '15 at 21:55
  • @Lzoesch I'm sorry. There you go, I gave you some ups – wobsoriano Sep 21 '15 at 07:08