CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: mySQL connection test on install...  (Read 227 times)

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 532
  • Karma: +5/-0
    • GrumpyYoungMan
mySQL connection test on install...
« 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?
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

John_Betong

  • Full Member
  • ***
  • Posts: 165
  • Karma: +16/-1
    • The Fastest Joke Site On The Web
Re: mySQL connection test on install...
« Reply #1 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(...);
Retired in the City of Angels where the weather suits my clothes

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 714
  • Karma: +132/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: mySQL connection test on install...
« Reply #2 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.
I'll fix every flaw, I'll break every law, I'll tear up the rulebook if that's what it takes. You will see, I will crush this cold machine.

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 532
  • Karma: +5/-0
    • GrumpyYoungMan
Re: mySQL connection test on install...
« Reply #3 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! :)
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

John_Betong

  • Full Member
  • ***
  • Posts: 165
  • Karma: +16/-1
    • The Fastest Joke Site On The Web
Re: mySQL connection test on install...
« Reply #4 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
« Last Edit: 20 Mar 2021, 03:20:18 am by John_Betong »
Retired in the City of Angels where the weather suits my clothes

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 532
  • Karma: +5/-0
    • GrumpyYoungMan
Re: mySQL connection test on install...
« Reply #5 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!
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 714
  • Karma: +132/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: mySQL connection test on install...
« Reply #6 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.
I'll fix every flaw, I'll break every law, I'll tear up the rulebook if that's what it takes. You will see, I will crush this cold machine.

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 532
  • Karma: +5/-0
    • GrumpyYoungMan
Re: mySQL connection test on install...
« Reply #7 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...
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

John_Betong

  • Full Member
  • ***
  • Posts: 165
  • Karma: +16/-1
    • The Fastest Joke Site On The Web
Re: mySQL connection test on install...
« Reply #8 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;

« Last Edit: 21 Mar 2021, 01:43:25 am by John_Betong »
Retired in the City of Angels where the weather suits my clothes

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 714
  • Karma: +132/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: mySQL connection test on install...
« Reply #9 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.
I'll fix every flaw, I'll break every law, I'll tear up the rulebook if that's what it takes. You will see, I will crush this cold machine.

 

SMF spam blocked by CleanTalk

Advertisement