php 警告:mysqli_select_db() 期望参数 1 为 mysqli,给定 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35472867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: mysqli_select_db() expects parameter 1 to be mysqli, null given
提问by Michael Kinder
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.
警告:mysqli_select_db() 期望参数 1 为 mysqli,第 4 行 C:\xampp\htdocs\MyWebsite\TechanexSiteBase\connect.php 中给出的 null。
回答by Omal Perera
Complete Guide
完整指南
Note:
注意:
- With mysqli you can specify the Database Namein
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()); } ?>
- 随着的mysqli您可以指定数据库名称中
mysqli_connect()
您必须使用
mysqli_connect_error()
,not mysqli_error()
来从 中获取错误mysqli_connect()
,因为后者要求您提供有效的连接。<?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
只需复制和粘贴 & 然后更改数据库名称
回答by Your Common Sense
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, stringgiven
.
问题正文没有意义,因为代码与错误消息不一致。对于存在的代码,错误消息应该是.Warning: mysqli_select_db() expects parameter 1 to be mysqli, stringgiven
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 properguide on How to connect using mysqli:
除此之外,这个问题似乎吸引了许多真正从问题标题中得到错误的访问者。对于他们来说,这里有一个关于如何使用 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 errorlike this. As well as many other errorsthat would be induced by the code suggested in the other answers.
通过遵循此例程,人们永远不会遇到这样的错误。以及其他答案中建议的代码会引起的许多其他错误。
Important advantages of this code are
此代码的重要优点是
- as you can see, there is no needto
call mysqli_select_db()
function at all, as the database name could be supplied already inmysqli_connect()
. - the proper error reportingmode is set, and therefore the actual error messagewill be reported, explaining why
mysqli_connect()
returnednull
, hence you'll be able to fix the error. - the proper charsetmust be always set
- and the connection errorshould be handled properly. Some mysqli internals is not what you really want for the site visitors to see.
- 你可以看到,有没有必要到
call mysqli_select_db()
功能可言,作为数据库名称,可以在已提供mysqli_connect()
。 - 设置了正确的错误报告模式,因此将报告实际的错误消息,解释
mysqli_connect()
返回的原因null
,因此您将能够修复错误。 - 必须始终设置正确的字符集
- 并且应该正确处理连接错误。某些 mysqli 内部结构并不是您真正希望站点访问者看到的。
The detailed explanation of all these issues could be found in the article linked above.
所有这些问题的详细解释可以在上面链接的文章中找到。
回答by Shaikhul Saad
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.
这里,localhost=服务器地址;root=用户名;“”=密码;帐户=您的数据库名称。
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.
?>
回答by Farhad Misirli
You can add your db name into mysqli_connect("localhost", "root", "", "account");
as fourth element
您可以将您的数据库名称添加mysqli_connect("localhost", "root", "", "account");
为第四个元素
回答by Dhruv Jain
Yuu can create mysqli connection as follows:
Yuu 可以按如下方式创建 mysqli 连接:
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}