DOCS

Connecting to a database from PHP

A PDO and mysqli connection example using localhost, and the most common connection errors.

Aktualisiert 05.10.2026. 1 Min. Lesezeit

Scripts running on your hosting connect to the database through the localhost host. You need the details from New MySQL database: the full database name, user and password.

php
<?php
$dsn = 'mysql:host=localhost;dbname=scyhost1_shop;charset=utf8mb4';
try {
    $pdo = new PDO($dsn, 'scyhost1_user', 'PASSWORD', [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    ]);
} catch (PDOException $e) {
    error_log($e->getMessage());
    exit('Database connection error.');
}

mysqli#

php
<?php
$db = new mysqli('localhost', 'scyhost1_user', 'PASSWORD', 'scyhost1_shop');
if ($db->connect_error) { error_log($db->connect_error); exit('Error.'); }
$db->set_charset('utf8mb4');

Most common errors#

  • Access denied for user: wrong password or the user was not added to the database. Check Current Databases in MySQL Databases.
  • Unknown database: the account prefix is missing from the name.
  • Could not find driver: the extension is not enabled, see PHP extensions.

Errors are written to a log, see PHP errors and logs.

War dieser Artikel hilfreich?

Verwandte Artikel

Still need a hand?

Open a ticket and we will reply by email. The whole team sees your request.