CutCodeDown

Web Development => PHP => Topic started by: GrumpyYoungMan on 19 Mar 2021, 09:12:53 am

Title: mySQL connection test on install...
Post by: GrumpyYoungMan on 19 Mar 2021, 09:12:53 am
Well...

Code: [Select]
function database_Test($host, $user, $password, $database)
{
    $charset = 'utf8mb4';
    $dsn = "mysql:host=$host;dbname=$database;charset=$charset";
    $options = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];

    try {
        $pdo = new PDO($dsn, $user, $password, $options);
        return true;
    } catch (\PDOException $e) {
        return false;
    }
}

What is the best way (practice) to test the MySQL connection on installation before continuing to write the configuration file?
Title: Re: mySQL connection test on install...
Post by: John_Betong on 19 Mar 2021, 06:51:43 pm
Instead of just returning false in the try/catch block, add the following to display all the errors.
Code: [Select]
echo ‘<pre>’; // add line feeds
print_r( $e );
echo ‘</pre>’;
return false;


Test by passing incorrect parameters to database_Test(...);
Title: Re: mySQL connection test on install...
Post by: Jason Knight on 20 Mar 2021, 02:46:15 am
Write the what now?

I don't know what you're "testing", but given that a opened PDO connection doesn't terminate until scripting ends, this is all-around a bad idea.

Just like all those garbage variables for nothing, double quoted strings with inlined variables, etc, etc.
Title: Re: mySQL connection test on install...
Post by: GrumpyYoungMan on 20 Mar 2021, 02:57:57 am
I want to test the MySQL details as part of the installation process... and if the details are correct then write those details to a file and continue with the install process.

You say it’s a bad idea, but have you got a better idea, please?

The connection staying opening isn’t too much of a problem as the next stage would be to populate the database? And if it fails there would be no connection?

Thank you John and Jason! :)
Title: Re: mySQL connection test on install...
Post by: John_Betong on 20 Mar 2021, 03:13:34 am
@GrumpyYoungMan,

My previous reply was tapped on a tablet and untried.

The following has been tried and I think gives the required results:

Code: [Select]
      echo '<pre>';
        # echo '<hr>';
        # print_r($e ); // TOO MUCH INFO
        echo '<hr>';
        print_r($e->getMessage() );
        echo '<hr>';
       print_r($e->errorInfo );
      echo '</pre>'; 
        return false;

RESULTS:
Code: [Select]

[pre]
[hr]
SQLSTATE[HY000] [1045] Access denied for user 'rootX'@'localhost' (using password: YES)
[hr]
Array ( [0] => HY000 [1] => 1045 [2] => Access denied for user 'rootX'@'localhost' (using password: YES) ) [/pre]/var/www/connect-learning.tk/public_html/index.php ==> 39
Title: Re: mySQL connection test on install...
Post by: GrumpyYoungMan on 20 Mar 2021, 01:02:47 pm
Thanks for your suggestion John, but I wanted to just return to the form with an error just saying that the details are incorrect - this is coming from a different function which is calling the database_Test function - which is why I set it to return true and false!
Title: Re: mySQL connection test on install...
Post by: Jason Knight on 20 Mar 2021, 02:20:07 pm
I want to test the MySQL details as part of the installation process... and if the details are correct then write those details to a file and continue with the install process.
Not something I'd ever let an installer set up. It's one of the biggest security flaws found in many systems, since the file and directory would then need write permissions when it should remain 644.

That's something you manually edit BEFORE you upload, not something you set up on the fly. If you're using a system that does go full Gungan and does it during install, you go in and make bloody well sure it's locked after.

Rather than worry about that after, I'd do it before as a manual edit.
Title: Re: mySQL connection test on install...
Post by: GrumpyYoungMan on 20 Mar 2021, 03:34:08 pm
I want to test the MySQL details as part of the installation process... and if the details are correct then write those details to a file and continue with the install process.
Not something I'd ever let an installer set up. It's one of the biggest security flaws found in many systems, since the file and directory would then need write permissions when it should remain 644.

That's something you manually edit BEFORE you upload, not something you set up on the fly. If you're using a system that does go full Gungan and does it during install, you go in and make bloody well sure it's locked after.

Rather than worry about that after, I'd do it before as a manual edit.
So, your not a fan of the way this forum is installed then?

That does make total sense - so, just have the installer check for the connection and then continue, if no connection just exit the script with a warning?

The only problem is not everyone who has a website would be confident enough to manually edit a file which is a different discussion...
Title: Re: mySQL connection test on install...
Post by: John_Betong on 21 Mar 2021, 01:32:36 am
Quote
@GrumpyYoungMan,
Thanks for your suggestion John, but I wanted to just return to the form with an error just saying that the details are incorrect - this is coming from a different function which is calling the database_Test function - which is why I set it to return true and false!

Instead of having another function to test creating a PDO Object I would set a Class Public $PDO variable and populate the variable from inside the function:

Change the function return types from bool true/false to string and $->getMessage();

// When you "return to the form" test for and if empty sting then no problems otherwise display the function $result;

Title: Re: mySQL connection test on install...
Post by: Jason Knight on 21 Mar 2021, 02:34:54 am
So, your not a fan of the way this forum is installed then?
No, I'm really not. Probably because back on version 1.4 it bit everyone who had it installed OOB that way, as part of the equally derpy trusting of name="something[]" they did that resulted in avatars being run as code.

The only problem is not everyone who has a website would be confident enough to manually edit a file which is a different discussion...
I consider it a filter. Anyone QUALIFIED to install something like this should be able to handle adding a few strings to ONE file.

The mollycoddling of the feeble-minded always runs the risk of false simplicity.