Question

I want to connect to a VPS server with the following code. What is wrong with it?

<?php
$con = mysqli_connect("A.B.C.D", "user", "pass", "db1");
if (!$con)  {
    die("Could not connect: ".mysqli_errno());
}

$sql = "SELECT * FROM table1";

$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
extract($row);

echo $name;

?>

The above code gives the error : Could not connect:

However, when I use "localhost" and run this script on a copy of that database and table that I have in my laptop, it runs smoothly and echoes the name from the first record in the table.

Why am I not being able to connect to my VPS?

Was it helpful?

Solution

Make sure MySQL has enabled outside access, you probably don't have access from remote machines to the VPS.

UPDATE: Do you have access to phpmyadmin or a tool like that?

OTHER TIPS

Check the following;

  • MySql is allowing network connections, other than localhost
  • Your firewall is not blocking your MySQL Port
  • The user you are connecting with, has either @% access, or your IP
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top