Warning: mysqli_select_db() expects parameter 1 to be mysqli, null given

佐手、 提交于 2019-12-29 09:02:28

问题


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.


回答1:


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




回答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.




回答3:


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.
?>



回答4:


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




回答5:


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();
}


来源:https://stackoverflow.com/questions/35472867/warning-mysqli-select-db-expects-parameter-1-to-be-mysqli-null-given

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!