gpt4 book ai didi

php - 连接失败: Can't connect to MySQL server on 'localhost' php

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

我是 php 和 ajax 新手,我运行了一个简单的应用程序,它不起作用,我不知道为什么。我正在使用mamp,Windows 8.1。

这是 php 文件:

 <?php

$grId = $_GET["groupId"];
$limit = $_GET["limit"];

if ($limit <= 0) {
$limit = 10;
}

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "productsdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, displayName, role, groupId FROM user where groupId = ? limit ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $grId, $limit);
$stmt->execute();
$stmt->bind_result($id, $displayName, $role, $groupId);

$users = array();

while($stmt->fetch()) {
$user = new StdClass();
$user->id = $id;
$user->displayName = $displayName;
$user->role = $role;
$user->groupId = $groupId;

array_push($users, $user);
}

echo json_encode($users);

$stmt->close();
$conn->close();
?>

和 html 文件:

<html>
<head>
<script src="jquery-2.1.4.js"></script>
<script>

$(document).ready(function () {
$('#students').hide();
$("#getStudentsButton").click(function () {
$.ajax({
dataType: "json",
type: "GET",
url: "getStudentsJson.php",
data: {groupId: 1, limit: 7},
success: renderTable
});
});

function renderTable(data) {
var trHTML = '';
$.each(data, function (i, item) {
trHTML += '<tr><td>' + item.id + '</td><td>' + item.displayName + '</td><td>'
+ item.role + '</td> <td> ' + item.groupId + ' </td></tr>';
});

$('#students').append(trHTML);
$('#students').show();
}

});
</script>
</head>

<body>

The list of students:
<div id="maindiv">
<table id="students" border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Role</th>
<th>GroupId</th>
</tr>

</table>

</div>
<input id="getStudentsButton" type="button" value="Load students"/>

</body>
</html>

我写:http://localhost/nameOfFolder/getStudentsJson.php在浏览器中,我收到错误:

Connection failed: Can't connect to MySQL server on 'localhost' (10061)

现在我认为错误可能是在安装 Mamp 之前我也有 Mysql。我在 phpmyadmin 中创建了一个名为 productsdb 的新数据库,其中包含 4 列。我认为这使得 phpmyadmin 和我的数据库中的数据库存在一些冲突mysql。我在 php 文件中编写的名称 localhost 正急切地尝试连接到我的本地 mysql,这是我在安装 mamp 之前所拥有的。

有什么办法可以解决这些问题吗?

最佳答案

The error (2003) Can't connect to MySQL server on 'server' (10061) indicates that the network connection has been refused. You should check that there is a MySQL server running, that it has network connections enabled, and that the network port you specified is the one configured on the server.

Manual

这可能是服务器未运行(如果您可以在 PHPMyAdmin 中看到相关信息,则不太可能)或可能是防火墙问题。您表示您使用的是 Windows,因此我将首先禁用防火墙并测试连接,然后完成 MySQL 手册中提供的 list 。

关于php - 连接失败: Can't connect to MySQL server on 'localhost' php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37309562/

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