1

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/

  • 1
    **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 12 '20 at 19:47
  • 1
    **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Sep 12 '20 at 19:47
  • 3
    The error says that your PHP is unreachable not the MySQL server – Dharman Sep 12 '20 at 19:48

0 Answers0