gpt4 book ai didi

mysql - PHP/MySQL – 连接到服务器时出错

转载 作者:行者123 更新时间:2023-11-29 12:05:25 24 4
gpt4 key购买 nike

我使用的是 Mac OSX,并且安装了 MAMP,它正在运行我的本地主机服务器。但是,运行 PHP 代码会出现一些问题,因为网站在连接服务器时不断返回错误。我不知道这有什么问题;也许有人可以在这里帮助我。

PHP 代码:

<?php

$connection = mysql_connect("localhost", "user", "password") or die ("There was an error when connecting to the server");
mysql_select_db("topaz", $connection) or die ("There was an error when connecting to the database");

echo "

<body style='font-family: Helvetica Neue, Helvetica, Arial, sans-serif;'>

<div style='width: 80%; padding: 10px; border: 1px solid #000000; background-color: #ffffff;'>
<h1>Login</h1>
</div>

</body>
";

?>

PHPMyAdmin 设置: enter image description here

MAMP 端口设置: enter image description here

错误: enter image description here

最佳答案

您需要通过控制面板中指定的端口建立连接 - 默认情况下,mysql_connect() 将尝试通过端口 3306 连接到您的 MySQL 主机。

mysql_connect的第一个参数(host)中指定使用哪个端口:

$connection = mysql_connect("localhost:8889", "user", "password") or die ("There was an error when connecting to the server");
// Here ^^^^^

强制说明:不要使用 mysql_* 函数,因为它们已被弃用。请改用 PDO 或 mysqli_*。

关于mysql - PHP/MySQL – 连接到服务器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31566169/

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