简体   繁体   中英

Connection failed: Can't connect to MySQL server on 'localhost' php

I'm new to php & ajax and I run a simple application, it doesn't work and I don't know why. I'm using mamp, windows 8.1.

Here are the php file:

 <?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();
?>

and the html file:

<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>

I write: http://localhost/nameOfFolder/getStudentsJson.php in the browser and I receive the error:

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

Now I think the error can be that before installing Mamp I also had Mysql.I have made a new database in phpmyadmin named productsdb with the 4 columns.I think that made there is some conflicts regarding the database from phpmyadmin and my database mysql. And the name localhost that I have written in php file is axiously trying to connect to my local mysql, the one that I have before the one installed with mamp.

Any solution to fix these issues?

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

This could either be the server is not running (not very likely if you can see the related information in PHPMyAdmin) or possible a firewall issue. You indicated you are on windows so I would start by disabling the firewall and testing connectivity then work through the checklist they provide in the MySQL manual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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