Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)
edited by

I am new to Mysqli_* and I am getting these following 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());

?> 

1 Answer

0 votes
by (12.7k points)

Regarding this error, I can see that your arguments are in the wrong order. The connection comes first, check here for more info.

<?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');

}

?>

Want to be a SQL expert? Come and join this SQL Certification course by Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jan 3, 2021 in SQL by Appu (6.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...