Connection refused in: How to connect to a MySQL database with PHP
I am currently working in the PHP Programming Basics course and I have come accross a problem when trying to connect to the database through the PHP syntax provided for:
Chapter 9: Working With the (MySQL) Database
Section 8: How to connect to a MySQL database with PHP.
The first error I got was Referenced entity is deprecated
for the mysql_ ()
functions, and found that PHP 7 had to use mysqli_ ()
instead.
Then I got the following error:
Warning: mysqli_connect(): (HY000/2002): No such file or directory in /app/test.php
I then found I had to add the correct port to the $server variable's string as the default port that it was expecting is not the correct one for my database. I then changed it to:
$server = 'localhost:32770';
Then I received this error:
Warning: mysqli_connect(): (HY000/2002): Cannot assign requested address in /app/test.php
I then googled and found that if I set a permanent connection to the server by using the prefix 'p:' to the string, and changed 'localhost' to it's ip counterpart of '127.0.0.1' it might resolve it.
This is the new error I got:
Warning: mysqli_connect(): (HY000/2002): Connection refused in /app/test.php
This is my current setup:
"appserver": {
"type": "php",
"version": "7.0",
"via": "apache",
"webroot": ".",
"urls": [
"https://localhost:32768",
"http://localhost:32769",
"http://php-prog-basics.lndo.site",
"https://php-prog-basics.lndo.site"
]
},
"database": {
"type": "mariadb",
"version": "10.1",
"creds": {
"user": "lamp",
"password": "lamp",
"database": "lamp"
},
"internal_connection": {
"host": "database",
"port": 3306
},
"external_connection": {
"host": "localhost",
"port": "32770"
}
}
}
This is my code for test.php:
// Replace with the username and password you used for PhpMyAdmin.
$server = "p:127.0.0.1:32770";
$username = 'my_databaseuser';
$password = 'pass';
$database = 'my_database';
// Let's connect!
$connection = mysqli_connect($server, $username, $password);
// If we can't connect, we should print out the error:
if (!$connection) {
die('<strong>You were not able to connect to your database because ' . mysqli_error() . '</strong>');
}
// To start querying, we need to select a database.
mysqli_select_db($database);
// And let's do our first query.
$result = mysqli_query("SELECT * FROM people");
//$result = mysql_query("SELECT name, shoe_size FROM people WHERE shoe_size > 8");
//$result = mysql_query("SELECT * FROM people WHERE shoe_size > 8 AND name LIKE '%ee%'");
//$result = mysql_query("SELECT name FROM people ORDER BY name ASC");
// Use mysql_fetch_array to pull each row into an array.
$output = '';
while ($row = mysqli_fetch_array($result)) {
foreach ($row as $key => $val) {
$output .= $key . ' = ' . $val . '<br />';
}
$output .= '<br />';
}
print $output;
?>
Can someone please advise on what is going on? Why can't I connect to the database that I have already created?
Also just to point out the
"password": "lamp",
"database": "lamp"
was for the primary database.
The custom one that is built for the people table in the course is:
"password": "pass",
"database": "my_database"