我是Mysqli_ *的新手,我收到了这些错误:
警告:mysqli_select_db()期望参数1为mysqli,string 在第11行的D:\ Hosting \ 9864230 \ html \ includes \ connection.php中给出
警告:mysqli_error()正好需要1个参数,给出0 第13行的D:\ Hosting \ 9864230 \ html \ includes \ connection.php
数据库选择失败:
<?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());
}
?>
答案 0 :(得分:47)
你的论点顺序错误。首先根据docs
连接<?php
require("constants.php");
// 1. Create a database connection
$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
die("Database connection failed");
}
// 2. Select a database to use
$db_select = mysqli_select_db($connection, DB_NAME);
if (!$db_select) {
die("Database selection failed: " . mysqli_error($connection));
}
?>
答案 1 :(得分:4)
// 2. Select a database to use
$db_select = mysqli_select_db($connection, DB_NAME);
if (!$db_select) {
die("Database selection failed: " . mysqli_error($connection));
}
你向mysqli_select_db()
的参数顺序倒退了。 mysqli_error()
要求您提供连接参数。 mysqli_XXX与mysql_XXX不同,这些参数不再是可选的。
另请注意,使用mysqli,您可以在mysqli_connect()
中指定数据库:
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if (!$connection) {
die("Database connection failed: " . mysqli_connect_error();
}
必须使用mysqli_connect_error()
而不是mysqli_error()
才能从mysqli_connect()
获取错误,因为后者要求您提供有效的连接。