Connection refused in: How to connect to a MySQL database with PHP

No replies
juangong
Offline
Last seen: 37 weeks 1 day ago
Joined: 2017-12-22

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:

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

?>

Also just to point out the

"user": "lamp",
 "password": "lamp",
 "database": "lamp"

was for the primary database.

The custom one that is built for the people table in the course is:

"user": "my_databaseuser",
 "password": "pass",
 "database": "my_database"

Can someone please advise on what is going on? Why can't I connect to the database that I have already created?

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.