I am having some issues with connecting my MySQL table to my php connection script. I am receiving this error whenever I attempt to test the code. (I am using Angular, MySQL Workbench, and php).
zone-evergreen.js:2845 POST http://localhost/angular_admin/php/login.php net::ERR_CONNECTION_REFUSED
The code that is attempting to make the connection with the MySQL server is the database.php script.
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Content-Type: application/json; charset=UTF-8");
$db_host = 'localhost:3306';
$db_username = 'root';
$db_password = '';
$db_name = 'users';
$mysqli = new mysqli($db_host, $db_username, $db_password,$db_name);
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
?>
I manually entered a test record within side of my table on the MySQL: test, test@website.com, and test. I used that to login when I received the error listed above. My login.php script looks like this:
<?php
include_once("database.php");
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
$pwd = mysqli_real_escape_string($mysqli, trim($request->password));
$email = mysqli_real_escape_string($mysqli, trim($request->username));
$sql = "SELECT * FROM users where email='$email' and password='$pwd'";
if($result = mysqli_query($mysqli,$sql))
{
$rows = array();
while($row = mysqli_fetch_assoc($result))
{
$rows[] = $row;
}
echo json_encode($rows);
}
else
{
http_response_code(404);
}
}
?>
And of course my users.ts script looks like this:
export class Users {
public Id: number;
public name: string;
public pwd:string;
public email:string;
constructor(Id:number,name: string,pwd:string,email:string) {
this.Id = Id;
this.name = name;
this.pwd = pwd;
this.email = email;
}
}
I have verified that my MySQL Workbench is running as: root, localhost:3306, the name of the schema is users (my database.php script may be looking for the wrong database name?). Is there any steps I am missing to ensure that the database is accessible by the php script? I am running the angular server using Node.js following the steps mentioned on this article https://fahmidasclassroom.com/register-and-login-system-using-angular-8-php-and-mysql/