警告:mysqli_select_db()期望参数1为mysqli,给定null [英] Warning: mysqli_select_db() expects parameter 1 to be mysqli, null given

查看:107
本文介绍了警告:mysqli_select_db()期望参数1为mysqli,给定null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直收到错误消息.

<?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:

警告:mysqli_select_db()期望参数1为mysqli,在第4行的C:\ xampp \ htdocs \ MyWebsite \ TechanexSiteBase \ connect.php中给出空值.

Warning: mysqli_select_db() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\MyWebsite\TechanexSiteBase\connect.php on line 4.

推荐答案

问题正文没有意义,因为代码与错误消息不一致.对于存在的代码,错误消息应该是Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given.

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, string given.

此外,该问题似乎吸引了许多真正从问题标题中发现错误的访问者.对于他们来说,这是有关如何使用mysqli进行连接的完整且正确指南:

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 proper guide on How to connect using 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 error like this. As well as many other errors that would be induced by the code suggested in the other answers.

此代码的重要优点是

  • 如您所见,call mysqli_select_db()功能完全不需要 ,因为数据库名称可以在mysqli_connect()中提供.
  • 设置
  • 正确的错误报告模式,因此将报告实际错误消息,解释为什么mysqli_connect()返回null的原因,因此您将能够解决错误.
  • 必须始终设置
  • 正确的字符集
  • 连接错误应得到正确处理.某些mysqli内部构件不是您真正想要的站点访问者.
  • as you can see, there is no need to call mysqli_select_db() function at all, as the database name could be supplied already in mysqli_connect().
  • the proper error reporting mode is set, and therefore the actual error message will be reported, explaining why mysqli_connect() returned null, hence you'll be able to fix the error.
  • the proper charset must be always set
  • and the connection error should be handled properly. Some mysqli internals is not what you really want for the site visitors to see.

所有这些问题的详细说明可以在上面链接的文章中找到.

The detailed explanation of all these issues could be found in the article linked above.

这篇关于警告:mysqli_select_db()期望参数1为mysqli,给定null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆