Retrospect
Retrospect

Reputation: 19

How to Connect to MySQL Server and access a pre-defined database from Remote Host in PHP?

I have different PHP files set up, along with an HTML file, of which my 1st PHP "signguestbook.php" seeks to connect to a remote MySQL database that's within a MySQL database server of which the database connection fails to establish over and over again. My PHP file, 'signguestbook.php', contains the code necessary to perform this function, as I want my program to (upon having the user input their credentials in my 'guestbook.html' file, use a pre-set MySQL database to create a table to enter in the user details. Unfortunately, this results in a blank webpage for the "signguestbook.php", in which I get an error message saying "HTTP ERROR 500", along with "www.remotesite.com is currently unable to handle this request".

signguestbook.php

if (empty($_POST['first_name']) || empty($_POST['last_name'])) { // checks if firstname and lastname fields are empty
    echo "<p align='center' >You must enter your first name and last name. Click your browser's Back button to return to the Guest Book form</p>";
} 
else {
    $user = "aCegikmoqsuwy"; // username provided by my administrator
    $password = "a1b2c3d4e5f6"; // password provided by my administrator
    $host = "www.remotesite.com"; // hostname of web server, but hostname for mysql user is "localhost'?

    $DBconnect = @mysqli_connect($host, $user, $password); // creates the database connection
    if ($DBconnect === FALSE) {
        echo "<p>ERROR. Unable to connect to the server.</p>" . "<p>Error code" . mysqli_errno() . ":" . mysqli_error() . "</p>";
    } else { 
        $DBName = "aCegikmoqsuwy_block1"; // pre-defined database, can only create tables within it
        if (!@mysqli_select_db($DBconnect, $DBName)) { // verifies whether database is not exited
            $SQLString = "USE DATABASE $DBName"; // This command uses this MySQL database
            $QueryResult = @mysqli_query($DBconnect, $SQLString);

I have my HTML code in a separate file, called guestbook.html:

<html>
<title>
    Guest Book
</title>
<body>
<h2 align="center">Please enter your name to sign our Guest Book.<h2>
        <form method="POST" action="signguestbook.php">
            <table align="center">
                <tr>
                    <td>First Name:</td>
                    <td><input type="text" name="first_name" /></td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td><input type="text" name="last_name" />
                </tr>
                <tr>
                    <td colspan=2 align="center"><input type="Submit" value="Submit" />
                </tr>
            </table>
        </form>
        <p align="center"><a href="showguestbook.php">Show Guest Book</a>
</body>
</html>

I use VS Code to connect to the remote web server via SSH (with my hostname as "www.remotesite.com", username, and password credentials). I also use MobaXterm to not only setup a session via SSH (with the same credentials mentioned before), but I also use the "mysql -u username -p" command (password is same as that used to login into session via SSH) to enter into the MySQL command line, where I can actually access the database and create tables and perform queries there. Yet, my code that I showed above already has commands designed to do this. Anyone knows what I'm doing wrong, and how to solve this issue?

Upvotes: 0

Views: 33

Answers (0)

Related Questions