H
H
Httdientee2021-08-18 12:38:14
PHP
Httdientee, 2021-08-18 12:38:14

PDO, I do not know how to do it right?

$localhost = 'localhost';
$dbname = 'us3456';
$username = 'root';
$password = '';

try {
    $dbh = new PDO('mysql:host='.$localhost.';dbname='.$dbname.'', $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) { echo 'error_connected';}


$dbh->query("SET NAMES utf8");
$dbh->query("SET CHARACTER SET utf8");
$dbh->query("SET character_set_client = utf8");
$dbh->query("SET character_set_connection = utf8");
$dbh->query("SET character_set_results = utf8");

try {
    $result = $pdo->query("SELECT 1 FROM us3456 LIMIT 1");
} catch (Exception $e) {
    $sql = "CREATE TABLE us3456 (
        `id` INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        `value` int(11) NOT NULL
        )";

    $dbh->query($sql);
}

I need to create a database if it does not exist, how to do it right? It just won't connect to the database right now because of this.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question