how to connect my mysql railway database to a php website?

milenaa052
TRIAL

a year ago

I'm trying, but it's giving this error: Uncaught mysqli_sql_exception: A connection attempt failed because the connected component did not respond correctly after a period of time or the established connection failed because the connected host did not respond in C:\xampp\htdocs\Projects\ WorldOfGames\config.php:10 Stack trace: #0 C:\xampp\htdocs\Projetos\WorldOfGames\config.php(10): mysqli->__construct('monorail.proxy....', 'root', Object (SensitiveParameterValue), 'railway') #1 {main} thrown in C:\xampp\htdocs\Projetos\WorldOfGames\config.php on line 10

11 Replies

a year ago

Please show us what you have tried.


milenaa052
TRIAL

a year ago

<?php

    $dbHost = 'monorail.proxy.rlwy.net';
    $dbUsername = 'root';
    $dbPassword = 'PVrlYjcwddbqsFgjYZSFVeZfRXgO****';
    $dbName = 'railway';

    $conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

    if ($conn->connect_error) {
        echo "Error";
    } else {
        echo "Connection made successfully";
    }

?>

a year ago

You forgot the port for the database, and you should be using environment variables.


milenaa052
TRIAL

a year ago

Can you show me how to do it?


a year ago

Set your database related variables in your service variables and then use getenv() in code.


milenaa052
TRIAL

a year ago

I tried like this

<?php

$dbHost = getenv('monorail.proxy.rlwy.net' . ':52749');

$dbUsername = getenv('root');

$dbPassword = getenv('PVrlYjcwddbqsFgjYZSFVeZfRXgO****');

$dbName = getenv('railway');

$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName,);

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

} else {

echo "Connection made successfully";

}

?>

and gave this error:

Fatal error: Uncaught mysqli_sql_exception: Access denied for user ''@'localhost' (using password: NO) in C:\xampp\htdocs\Projetos\WorldOfGames\config.php:8 Stack trace: #0 C:\xampp\htdocs\Projetos\WorldOfGames\config.php(8): mysqli->__construct('', '', Object(SensitiveParameterValue), '') #1 {main} thrown in C:\xampp\htdocs\Projetos\WorldOfGames\config.php on line 8


a year ago

That would be incorrect. Please familiarize yourself with what environment variables are and how to use them as that would not be anything Railway specific.


milenaa052
TRIAL

a year ago

Could you show me what the code looks like? I saw something related to the .env file


a year ago

I'm sorry but we can't offer code help here.

Any online tutorial on how to use environment variables in php would suffice.


milenaa052
TRIAL

a year ago

I got it here, thank you very much for the tips :)


the3v
HOBBY

a year ago

Hey! Can you show me please or tell how do you fixed that error? please


how to connect my mysql railway database to a php website? - Railway Help Station