2

I keep getting an error message.

<?php

mysqli_connect("localhost", "root", "");
mysqli_select_db("account");

?>

Here is the error message I get when I try to load it in the browser:

Warning: mysqli_select_db() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\MyWebsite\TechanexSiteBase\connect.php on line 4.

Dharman
  • 30,962
  • 25
  • 85
  • 135
ressonix
  • 31
  • 1
  • 1
  • 10

5 Answers5

3

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 from mysqli_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

Omal Perera
  • 2,971
  • 3
  • 21
  • 26
  • Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) – Dharman Dec 27 '19 at 20:11
2

The question body makes no sense, as the code is inconsistent with the error message. For the code present, the error message would have been Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given.

That aside, it seems this question attracts many visitors who are genuinely getting the error from the question title. For them, here is a complete and proper guide on How to connect using mysqli:

$host = '127.0.0.1';
$db   = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
    $mysqli = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($mysqli, $charset);
} catch (\mysqli_sql_exception $e) {
     throw new \mysqli_sql_exception($e->getMessage(), $e->getCode());
}

By following this routine, one would never ever get an error like this. As well as many other errors that would be induced by the code suggested in the other answers.

Important advantages of this code are

  • as you can see, there is no need to call mysqli_select_db() function at all, as the database name could be supplied already in mysqli_connect().
  • the proper error reporting mode is set, and therefore the actual error message will be reported, explaining why mysqli_connect() returned null, hence you'll be able to fix the error.
  • the proper charset must be always set
  • and the connection error should be handled properly. Some mysqli internals is not what you really want for the site visitors to see.

The detailed explanation of all these issues could be found in the article linked above.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

You can establish your connection by a single line as follows.

<?php
$con = mysqli_connect("localhost", "root", "","account");
?>

Here, localhost=server address; root=user name; ""=password; account=your database name.

or

<?php
$con = mysqli_connect("localhost", "root", "","account");
?>

There is also another way like you tried:

<?php
$con = mysqli_connect("localhost", "root", ""); //connection
mysqli_select_db($con, "account"); //Mysqli_select_db() function expects exactly 2 parameters.
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Shaikhul Saad
  • 177
  • 2
  • 2
  • 12
0

You can add your db name into mysqli_connect("localhost", "root", "", "account"); as fourth element

-1

Yuu can create mysqli connection as follows:

$mysqli = new mysqli("localhost", "my_user", "my_password", "test");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
Dhruv Jain
  • 125
  • 4