21

I am new to Mysqli_* and I am getting these errors:

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in D:\Hosting\9864230\html\includes\connection.php on line 11

Warning: mysqli_error() expects exactly 1 parameter, 0 given in D:\Hosting\9864230\html\includes\connection.php on line 13

Database selection failed:

<?php
require("constants.php");

// 1. Create a database connection
$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
    die("Database connection failed: " . mysqli_error());
}

// 2. Select a database to use 
$db_select = mysqli_select_db(DB_NAME,$connection);
if (!$db_select) {
    die("Database selection failed: " . mysqli_error());
}
?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
user1551909
  • 461
  • 1
  • 4
  • 9
  • You have the wrong parameter order, set `$connection` before `DB_NAME` at your call to `mysqli_select_db()`. I don't think this question shows any search effort at all, try google or look at php.net next time. – Cyclonecode Dec 20 '12 at 19:35
  • @Marc B - same basic issue – John Conde Dec 20 '12 at 19:36
  • 1
    @JohnConde , the issue there is lack of error checking. – Barmar Dec 20 '12 at 19:38
  • 3
    Please learn about functions first before you use them, that includes learning about the meaning of each parameter as well as learning about the return value and the type of error handling that is used with the function. – hakre Dec 20 '12 at 19:41
  • Please read https://stackoverflow.com/questions/58808332/should-we-ever-check-for-mysqli-connect-errors-manually – Dharman Dec 18 '19 at 07:17

2 Answers2

60

Your arguments are in the wrong order. The connection comes first according to the docs

<?php
require("constants.php");

// 1. Create a database connection
$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS);

if (!$connection) {
    error_log("Failed to connect to MySQL: " . mysqli_error($connection));
    die('Internal server error');
}

// 2. Select a database to use 
$db_select = mysqli_select_db($connection, DB_NAME);
if (!$db_select) {
    error_log("Database selection failed: " . mysqli_error($connection));
    die('Internal server error');
}

?>
Chris McKnight
  • 8,540
  • 4
  • 29
  • 31
  • Please read https://stackoverflow.com/questions/58808332/should-we-ever-check-for-mysqli-connect-errors-manually – Dharman Dec 18 '19 at 07:15
7
// 2. Select a database to use 
$db_select = mysqli_select_db($connection, DB_NAME);
if (!$db_select) {
    die("Database selection failed: " . mysqli_error($connection));
}

You got the order of the arguments to mysqli_select_db() backwards. And mysqli_error() requires you to provide a connection argument. mysqli_XXX is not like mysql_XXX, these arguments are no longer optional.

Note also that with mysqli you can specify the DB in mysqli_connect():

$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if (!$connection) {
  die("Database connection failed: " . mysqli_connect_error();
}

You must use mysqli_connect_error(), not mysqli_error(), to get the error from mysqli_connect(), since the latter requires you to supply a valid connection.

Barmar
  • 741,623
  • 53
  • 500
  • 612