简体   繁体   English

PHP 无法使用 mysqli_connect 连接到数据库

[英]PHP can't connect to database with mysqli_connect

i'm facing with this problem that when i'm trying connect to database with constants, then it's showing this in my VS editor: Undefined constant 'DB_HOST', 'DB_USER', 'DB_PASS', 'DB_NAME'.我面临这个问题,当我尝试使用常量连接到数据库时,它会在我的 VS 编辑器中显示:未定义的常量'DB_HOST'、'DB_USER'、'DB_PASS'、'DB_NAME'。

Here my code:这是我的代码:

<?php

$db['db_host'] = "localhost";
$db['db_user'] = "root";
$db['db_pass'] = "";
$db['db_name'] = "cms";

foreach($db as $key => $value){
     define(strtoupper($key), $value);
}

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

?>

I tried change我试过改变

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

to

$connection = mysqli_connect('DB_HOST', 'DB_USER', 'DB_PASS', 'DB_NAME');

After this step, my localhost was showing this error below:在这一步之后,我的本地主机在下面显示了这个错误:

Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known.警告:mysqli_connect():php_network_getaddresses:getaddrinfo 失败:不知道这样的主机。 in C:\xampp\htdocs\cms\includes\db.php on line 12在第 12 行的 C:\xampp\htdocs\cms\includes\db.php

Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known.警告:mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: 没有这样的主机是已知的。 in C:\xampp\htdocs\cms\includes\db.php on line 12在第 12 行的 C:\xampp\htdocs\cms\includes\db.php

Could you please help me solve this error, or tell me what i'm doing wrong你能帮我解决这个错误吗,或者告诉我我做错了什么

it seems to be an issue with the Visual Studio Code.这似乎是 Visual Studio Code 的问题。 The code works and connects to the db OK but shows DB_HOST etc as undefined with a wiggly red 'naughty' line.该代码可以正常工作并连接到 db OK,但将 DB_HOST 等显示为未定义,并带有一条红色的“顽皮”线。 So I got rid of the warning by defining the constants without a loop or associative array.所以我通过定义没有循环或关联数组的常量来摆脱警告。

define("DB_HOST", 'localhost');
define("DB_USER", 'root');
define("DB_PASS", '');
define("DB_NAME", 'cms');

Assuming you're developing locally (or MySQL/MariaDB is available on localhost port 3306), it looks like you need to determine which IP address you're using.假设您正在本地开发(或 MySQL/MariaDB 在 localhost 端口 3306 上可用),您似乎需要确定您正在使用哪个 IP 地址。 In command line, try:在命令行中,尝试:

c:\>ping localhost

You should see a response like:您应该会看到如下响应:

Pinging localhost with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Use the address stated in the reply (ie, 127.0.0.1 ).使用回复中所述的地址(即127.0.0.1 )。 Note that your localhost might not be set up right.请注意,您的本地主机可能设置不正确。 If you're using XAMPP, ensure that the MySQL service is running first.如果您使用的是 XAMPP,请确保首先运行 MySQL 服务。 Check the mysql_error.log file and see what that tells you.检查mysql_error.log文件,看看它告诉你什么。

<?php
    $db = [
        'db_host' => '127.0.0.1', /* or the ip address to the MySQL service */
        'db_port' => '3306',
        'db_user' => 'root',
        'db_pass' => '',
        'db_name' => 'cms'
    ];

    $con = new mysqli($db['db_host'], $db['db_user'], $db['db_pass'], $db['db_name'], $db['db_port']);

    if ($error = mysqli_connect_error()) {
        echo '<pre>' . print_r($error, TRUE) . '</pre>';
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM