-1

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);
?>    
Michael Doye
  • 8,063
  • 5
  • 40
  • 56
Florin Iuonas
  • 13
  • 1
  • 4
  • A single look into the documentation of the commands would have shown you the issue: http://php.net/manual/en/mysqli.select-db.php and http://php.net/manual/en/mysqli.query.php You are using the procedural style. Therefore the first argument must be the connection object. – arkascha Mar 14 '15 at 13:01
  • As far as I'm concerned; you can accept whatever answer you want. However, the one you originally accepted didn't fully outline all the errors and to me, doesn't qualify as the best or most complete answer for future visitors to the question. For instance; `MYSQL_ASSOC` missing the `I` and `mysqli_error()` requires DB connection be passed. Then the fact about the column name. WHY could you not have used that name, and how does MySQL interpret the minus sign as? *Ah!* – Funk Forty Niner Mar 14 '15 at 17:18

2 Answers2

2

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.

Thiago Elias
  • 939
  • 1
  • 6
  • 21
  • Error 1 is gone,but the other two? – Florin Iuonas Mar 14 '15 at 13:04
  • Sorry, I changed the answer and added the other two cases – Thiago Elias Mar 14 '15 at 13:06
  • Those 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. – Thiago Elias Mar 14 '15 at 13:09
  • it worked,now i have this error Could not get data: Champ 'E' inconnu dans field list – Florin Iuonas Mar 14 '15 at 13:09
  • I think the problem is your field name. I don't recommend you to use "E-mail" with special characters. try to change your database and queries to "email". It will be easier and will prevent to fall in errors like this. (you may use e_mail, if you want) – Thiago Elias Mar 14 '15 at 13:15
  • My answer were unaccepted ? As I remember, I were the first to answer and help. – Thiago Elias Mar 14 '15 at 13:41
  • 1
    *"My answer were unaccepted ? As I remember, I were the first to answer and help."* - Because, you didn't "explain" why their column name was failing including other aspects of the functions. There, the OP re-accepted your answer; happy now? Plus, just because your answer was put in "first", doesn't always mean it's the best answer in accordance with the question in its entirety. The OP has a choice to choose the best and more complete answer. – Funk Forty Niner Mar 14 '15 at 17:07
  • Ok, thanks for the advice, but whatever man, 15pts won't change my life at all. If OP want to mark your answer as right one, do it. Downvote my answer and be happy. – Thiago Elias Mar 14 '15 at 18:08
  • You're entirely welcome and it's not the 15 points that will change mine neither. People visiting the question need to know that the accepted answer wasn't accepted for the wrong reason. I will not downvote your answer and should you ever get one, it would not have come from me; I can assure you of that. When I post an answer for problematic code, I point out where all the faults were made, something that wasn't done in your answer. I'm telling you this in order for you to provide more complete answers for future questions. I'm just trying to help you give better answers; that's all. – Funk Forty Niner Mar 14 '15 at 23:18
2

There are a few things wrong with your code. This being a late answer, am submitting the following, indicating what is actually going on.

mysqli_select_db('catalogonline'); - it requires DB connection to be passed as the first parameter.

mysqli_select_db($conn, 'catalogonline');

or simply use all four parameters right away:

$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $db);

Then these parameters' variables in your query mysqli_query( $sql, $conn ) are inversed.

The connection comes first mysqli_query( $conn, $sql )

MYSQL_ASSOC that needs to be MYSQLI_ASSOC with the added I. You cannot mix mysql_ and mysqli_ functions together.

mysqli_error() requires connection be passed mysqli_error($conn)

string mysqli_error ( mysqli $link )

Then in your query, the E-mail column. MySQL is interpreting that as "E minus mail", thinking you want it to do math. It should either be wrapped in ticks, or rename it to either using an underscore E_mail or in one word Email.

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

$retval = mysqli_query( $conn, $sql  );
if(! $retval )
{
  die('Could not get data: ' . mysqli_error($conn));
}
while($row = mysqli_fetch_array($retval, MYSQLI_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);
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141