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

时间:2023-03-07
本文介绍了mysqli_select_db() 期望参数 1 是 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');
}

?>

这篇关于mysqli_select_db() 期望参数 1 是 mysqli,给定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Mysqli get_result 替代方案 下一篇:Mysqli 抛出“警告:mysqli_stmt_bind_param() 期望参数 1 为 mysqli_stmt,给

相关文章