一尘不染

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

php

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

警告:mysqli_select_db()期望参数1为mysqli,第11行的D:\ Hosting \ 9864230 \ html \
includes \ connection.php中给出的字符串

警告:mysqli_error()恰好需要1个参数,第13行的D:\ Hosting \ 9864230 \ html \ includes \
connection.php中给出的参数为0

数据库选择失败:

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

阅读 605

收藏
2020-05-26

共1个答案

一尘不染

您的论点顺序错误。根据文档,连接优先

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

?>
2020-05-26