skip to Main Content

After reading over a number of the similar topics here and on other various internet forums, I’m still having an issue with my PHP code which is throwing me for a loop… Here’s the error that the error_log file is giving me:

[30-Apr-2018 17:05:11 UTC] PHP Warning:  mysqli::mysqli(): (HY000/2002): Connection refused in /home/nicktric/public_html/index.php on line 12

And here is the current, and very simple, state of my code:

<!DOCTYPE html>
<html>
<body>

<?php
$servername = "127.0.0.1";
$username = "nicktric_admin";
$password = "########";
$dbname = "nicktric_routes";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT name, server, status FROM routeStatus";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> id: ". $row["name"]. " - Name: ". $row["server"]. " " . $row["status"] . "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?> 

</body>
</html>

I’ve tried the following so far:

  • Changing hostname between localhost, 127.0.0.1, and the server shared IP
  • Modifying the PHP socket to no avail, as I was unable to find the proper configuration to do so on CPanel

Additionally, some things to note are:

  • I am doing this off of CPanel web hosting
  • The database name, username, and password are all correct and linked to the database (Password covered for security)

2

Answers


  1. check mysqld is running and that your firewall is allowing the mysql database through.

    Edit your php.ini file and make sure extension extension=php_mysqli.dll is enabled on Windows or extension=php_mysqli.so on Linux.

    Try the answer here: Test Mysql

    • There are two settings you have to check for this to work (assuming
      you have MySQL server installed of course):
    • Check the value of mysql.default_socket in your PHP configuration.
    • Check the value of socket in your MySQL configuration file under the
      [mysqld] heading.
    • Those values have to be identical; if they’re not, change one to
      match the other and restart respective service.
    Login or Signup to reply.
  2. In /etc/mysql/my.cnf you should see this near the top of the file:
    
    [client]
    port          = 3306
    socket        = /var/run/mysqld/mysqld.sock
    Change socket to the location of your MemSQL socket file. By default, this is /var/lib/memsql/data/memsql.sock.
    
    hope that will work !
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search