gpt4 book ai didi

php - 没有这样的主机是已知的 - Localhost 无法识别

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:08 24 4
gpt4 key购买 nike

我是 php 新手。我在使用 PHP 连接和使用数据时遇到了问题。我已经仔细检查并查看了该网站上的信息,但没有找到太多信息。下面是错误和代码。

我正确输入了用户名和密码。我什至创建了一个新的用户名和密码来确保。我不知道为什么它不会连接到我的本地主机。我很乐意看到任何关于可能的错误的反馈,谢谢!

错误是:

Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\website\Practice\mysqli\connection.inc.php on line 26

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. ' in C:\xampp\htdocs\website\Practice\mysqli\connection.inc.php:26 Stack trace: #0 C:\xampp\htdocs\website\Practice\mysqli\connection.inc.php(26): PDO->__construct('mysql:host=$hos...') #1 C:\xampp\htdocs\website\Practice\mysqli\pdo.php(5): dbConnect('read') #2 {main} thrown in C:\xampp\htdocs\website\Practice\mysqli\connection.inc.php on line 26 $result = $conn->query($sql)or die(mysqli_error());

连接.ini.php

function dbConnect($usertype, $connectiontype = 'mysqli') {
$host = 'localhost';
$db = 'student';
if ($usertype == 'read') {
$user = 'user';
$pwd = 'pass';
}

elseif ($usertype == 'write') {
$user = 'root';
$pwd = 'password';
}
else {
exit('Unrecognized connection type');
}
//Connection Code
if ($connectionType == 'mysqli') {
return new mysqli($host, $user, $pwd, $db) or die ('Cannot open database');
}
else {

try {
return new PDO('mysql:host=$host;dbname=$db, $user, $pwd');
}
catch(PDOExecption $e) {
echo 'Cannot connect to database';
exit;
}
}
}
?>

mysqli.php

?php
require_once('connection.inc.php');
$conn = dbConnect('read');
$sql = 'SELECT * FROM guestbook';

$result = $conn->query($sql)or die(mysqli_error());
$numRows = $result->num_rows;
?>
<!DOCTYPE html>
<html>
<p> A total of <?php
echo $numRows;
?>
records were found.</p>
</html>

最佳答案

不解析单引号括起来的字符串,因此其中的变量不会被它们的值替换。在 DSN 字符串中使用双引号而不是单引号:

return new PDO("mysql:host=$host;dbname=$db, $user, $pwd");

关于php - 没有这样的主机是已知的 - Localhost 无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31432676/

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