gpt4 book ai didi

php_network_getaddresses : getaddrinfo failed:

转载 作者:行者123 更新时间:2023-11-29 04:45:06 26 4
gpt4 key购买 nike

我想连接sql数据库。我究竟做错了什么?请帮忙

常量.php

<php
// Database Constants
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "1234");
define("DB_NAME", "widget_corp");
?>

连接.php

<?php
require("constants.php");
//1. Create a database connection
$connection = mysql_connect('DB_SERVER','DB_USER','DB_PASS');
if(!$connection) {
die("Database connection failed: " . mysql_error());
}

//2. Select a database to use
$db_select = mysql_select_db('DB_NAME', $connection);
if(!$db_select) {
die("Database selection failed: " . mysql_error());
}

?>

最佳答案

首先,您的 constants.php 文件似乎在开始的 PHP 标记中有错字。您应该按如下方式更新文件:

<?php
// Database Constants
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "1234");
define("DB_NAME", "widget_corp");
?>

其次,常量名称不是字符串,它们是常量。您的 mysql_connect() 函数应如下所示:

$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);

同样,mysql_select_db() 应该是:

$db_select = mysql_select_db(DB_NAME, $connection);

您应该被告知,mysql_*() 函数家族现在已被弃用。您应该考虑使用 MySQLiPDO为了安全和长寿。

关于php_network_getaddresses : getaddrinfo failed:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20338235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com