I need confirmation of a successful PDO connection

I read a lot of posts about this and there seemed to be no consensus. I am writing a generic open source installer for web applications and I need to confirm a successful PDO connection. It should check the connection to the PDO and then write the actual config / init file. Part of the file record is working fine, but I can’t get a specific check that the PDO is really connected. He says that he is connected if the form is empty, and she says that it is connected if some information in the form is correct. How to get a clear indication of the correct pdo credentials? I will give you a (stripped down) version of what I am doing.

The form:

<H2>Please fill in your database credentials</H2>
<form class="form" action="" method="post">
  <label for="dbh">Database Host
  <input class="form-control" type="text" name="dbh"  value="<?php if (!empty($_POST['dbh'])){ print $_POST['dbh']; } ?>"></label><br><br>

  <label for="dbu">Database User
  <input class="form-control" type="text" name="dbu"  value="<?php if (!empty($_POST['dbu'])){ print $_POST['dbu']; } ?>"></label><br><br>

  <label for="dbp">Database Password
  <input class="form-control" type="text" name="dbp"  value="<?php if (!empty($_POST['dbp'])){ print $_POST['dbp']; } ?>"></label><br><br>

  <label for="dbn">Database Name
<input class="form-control" type="text" name="dbn"  value="<?php if (!empty($_POST['dbn'])){ print $_POST['dbn']; } ?>"></label><br><br>

Php

<?php
if (!empty($_POST)) {
    $dbh=$_POST['dbh']; //db host
    $dbu=$_POST['dbu']; //db username
    $dbp=$_POST['dbp']; //db password
    $dbn=$_POST['dbn']; //database name

    //If Testing
    if (!empty($_POST['test'])) {
        $dsn = "mysql:host=$dbh;dbname=$dbn;charset=utf8";
        $opt = array(
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
            );
        $pdo = new PDO($dsn, $dbu, $dbp, $opt);
     }

    //todo:  If Submitted, a bunch more logic here
}
?>
+4
source share
1

- , PDOException.

try {
    $pdo = new PDO($dsn, $dbu, $dbp, $opt);
} catch (PDOException $e) {
    // bad connection
}

, , , . , , mysql ( ), .

, (, foo;port=123) dsn, NULL ( e ).

/**
 * Return a PDO object if the connection options are correct, and if the connection is established. False otherwhise.
 */
function test_mysql_connection($host, $dbname, $username, $password = null) {
    if (empty($host) || empty($dbname) || empty($username)) {
        // those parameters MUST NOT be empty
        throw new InvalidArgumentException('Host, database name and username are required.')
    }

    try {
        $pdo = new PDO(build_mysql_dsn_safely($host, $dbname), $username, $password);
        return $pdo;
    } catch (PDOException $e) {
        // bad connection
        return false;
    }
}

/**
 * See how is it parsed here: https://github.com/php/php-src/blob/71c19800258ee3a9548af9a5e64ab0a62d1b1d8e/ext/pdo/pdo.c#L207
 */
function build_mysql_dsn_safely($host, $dbname = null, $charset = null, $port = null)
{
    static $bad_chars = array(';', '=', "\0");

    $vars = array_filter(array(
        'host' => $host,
        'dbname' => $dbname,
        'charset' => $charset,
        'port' => $port,
    ));

    foreach($vars as $param => $data) {
        foreach ($bad_chars as $bad_char) {
            if (strpos($data, $bad_char) !== false) {
                throw new InvalidArgumentException(sprintf(
                    'Connection options "%s" contains an invalid value.', $param
                ));
            }
        }
    }

    return 'mysql:'.implode(';', array_map(function($optkey, $optval) {
        return $optkey.'='.$optval;
    }, array_keys($vars), $vars));
}

if($pdo = test_mysql_connection($_POST['dbh'], $_POST['dbu'], $_POST['dbn'], $_POST['dbp'])) {
    // connection established
}
+5

Source: https://habr.com/ru/post/1625579/


All Articles