mysqli_select_db() expects parameter 1 to be mysqli, string given
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');
}
?>
Warning: mysqli_select_db() expects parameter 1 to be mysqli, null given
Complete Guide
Note :
- With mysqli you can specify the Database Name in
mysqli_connect()
You have to use
mysqli_connect_error()
,not mysqli_error()
, to get the
error frommysqli_connect()
, because the latter requires you to supply
a valid connection.<?php
// Creating a database connection
$connection = mysqli_connect("localhost", "root", "", "databse_name");
if (!$connection) {
die("Database connection failed: " . mysqli_connect_error());
}
// Selecting a database
$db_select = mysqli_select_db($connection, "databse_name");
if (!$db_select) {
die("Database selection failed: " . mysqli_connect_error());
}
?>
Just do copy and paste & Then change the database name
mysqli_select_db() expects parameter 1 to be mysqli, string given & mysqli_error() expects exactly 1 parameter, 0 given in DATABASE_CONNECT_ERROR
First of all you no need to have "" for your DB connection because you pass it as a variable
Second thing is In MYSQLI always Pass $connection
with your Connection.
And for a mysqli_error pass $connection to let server know you are looking for error in Database Connection.
Try this.
$connect = mysqli_connect($db_server, $db_user, $db_password)
or die( DATABASE_CONNECT_ERROR . mysqli_error($connect) );
mysqli_select_db($connect, $db_database)
or die( DATABASE_CONNECT_ERROR . mysqli_error($connect) );
Edit
either way you can try connect your DB As follow
$connect = mysqli_connect($db_server, $db_user, $db_password, $db_database);
// If no connection than Print error
if (mysqli_connect_errno())
{
echo "No Active DB Connection Please check: " . mysqli_connect_error();
}
Warning: mysqli_select_db() expects parameter 1 to be mysqli
Try This below code:
You have to pass first $conn
when select the DB, you have changed the parameter order.
<?php
// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'simple_login');
$conn = mysqli_connect(DBHOST,DBUSER,DBPASS);
$dbcon = mysqli_select_db($conn,DBNAME);
if ( !$conn ) {
die("Connection failed : " . mysqli_connect_errno());
}
if ( !$dbcon ) {
die("Database Connection failed : " . mysqli_connect_errno());
}
Hope this help!!
HtmlEncode UTF-8
Update (Solved): Adding <%@CODEPAGE=65001%> at the start of the page corrected the problem (http://technet.microsoft.com/en-us/library/bb742422.aspx)
Many thanks for all of the suggestions.
Related Topics
Does the Order of Class Definition Matter in PHP
Array to Object and Object to Array in PHP - Interesting Behaviour
How to Add Namespace to an Attribute with PHP's Simplexml
Php: Check for Duplicate Values in a Multidimensional Array
Mod_Rewrite, PHP and the .Htaccess File
Convert Datetime to String PHP
Http Protocol's Put and Delete and Their Usage in PHP
How to Enable --Enable-Soap in PHP on Linux
How to Call a PHP Function from Ajax
Mysqli_Select_Db() Expects Parameter 1 to Be MySQLi, String Given
How to Join Two Tables with Ssp.Class.Php
Dynamic Shipping Fee Based on Custom Radio Buttons in Woocommerce
How to Get &Curren to Display Literally, Not as an HTML Entity