I was able to get a PHP program to work on my local computer but not on my server. My code stops before echo 2;
and I don't know why. What does it mean when $conn = new mysqli($servername, $username, $password, $dbname);
doesn't work? Should it not have printed "Connection failed" in the if
statement? I don't really know how to debug PHP as when it doesn't work nothing shows in the chrome console or the page (at least the way I'm doing it now). Any PHP debugging tips for a newbie would be greatly appreciated.
Here are some things I have tried:
- The command
mysql -u username -p -h localhost
from my server (which is successful) - Checking if myDB exists with the proper columns and data (myDB with table
myGuests
with columnsid
,firstname
,lastname
and data1
,john
,doe
exists) - Adding more echo statements further down the program (still only
1
is echoed) - Installing php5.6-mysqlnd in case mysqli wasn't already there (no changes)
Here are somethings I'd like to try:
- Check the firewall
- See if I can access the database from outside my server?
Please help! Thank you :)
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
echo 1;
$conn = new mysqli($servername, $username, $password, $dbname);
echo 2;
// Check connection
if ($conn->connect_error) {
echo 3;
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();