phpmysqlselectmysqliinfo

Select from Database errors


I want to select some info from my database called Catalogonline and display it on a Html Page.But now my php code show this errors

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\wamp\www\insert12.php on line 14

And

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\insert12.php on line 15

And

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\wamp\www\insert12.php on line 18 Could not get data:

My code is

<?php
$dbhost = 'localhost';
$dbuser = 'Florin';
$dbpass = '######';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysqli_error());
}
$sql = 'SELECT id, Username, 
               Signup_Date, E-mail
        FROM membri';

mysqli_select_db('catalogonline');
$retval = mysqli_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysqli_error());
}
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
    echo "Tutorial ID :{$row['id']}  <br> ".
         "Title: {$row['Username']} <br> ".
         "Author: {$row['Signup_Date']} <br> ".
         "Submission Date : {$row['E-mail']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysqli_close($conn);
?>    

Solution

  • mysql_select_db expects two params, but you`re passing only one.

    The correct use is:

    mysqli_select_db(connection,dbname);
    

    try to change to

    mysqli_select_db($conn,'catalogonline');
    

    In mysqli_query( $sql, $conn );

    try to change to:

    mysqli_query( $conn, $sql );
    

    In mysqli_error();

    try to change to:

    mysqli_error($conn);
    

    These problems are happening, because when you're using mysqli commands, you must pass the connection object (in your case $conn), so it will be able to identify in what connection you'll execute the commands.