mysqli_select_db() 期望参数 1 是 mysqli,给定的字符串

2021-12-25 00:00:00 php mysqli

我是 Mysqli_* 的新手,但出现以下错误:

I am new to Mysqli_* and I am getting these errors:

警告:mysqli_select_db() 期望参数 1 是 mysqli,字符串在第 11 行的 D:Hosting9864230htmlincludesconnection.php 中给出

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in D:Hosting9864230htmlincludesconnection.php on line 11

警告:mysqli_error() 需要 1 个参数,0 给出D:Hosting9864230htmlincludesconnection.php 第 13 行

Warning: mysqli_error() expects exactly 1 parameter, 0 given in D:Hosting9864230htmlincludesconnection.php on line 13

数据库选择失败:

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

推荐答案

您的论点顺序错误.根据 docs

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

?>

相关文章