Create a database and connect an application to it

Create a MySQL database and user, confirm the login in phpMyAdmin, and connect a PHP script with PDO.

BeginnerAbout 15 minutes6 steps

What you’ll need

  • DirectAdmin access

You’ll create the database, prove the credentials work, and see a PHP page read from it — the foundation for any database-backed site.

Steps

  1. Step 1: Create the database and user

    Open Account Manager then MySQL Management → Create new Database. Name it demo, user demo, generate a password and save it. Note the full prefixed names DirectAdmin shows, such as acme_demo. How to create a MySQL database in DirectAdmin

  2. Step 2: Open phpMyAdmin

    Open Extra Features then phpMyAdmin and select your new database.

  3. Step 3: Create a table

    Open the SQL tab and run:

    CREATE TABLE notes (
      id INT AUTO_INCREMENT PRIMARY KEY,
      body VARCHAR(255) NOT NULL
    );
    INSERT INTO notes (body) VALUES ('Hello from MySQL');
  4. Step 4: Create a test script

    In System Info & Files then File Manager, create public_html/dbtest-7h2k.php:

    <?php
    $pdo = new PDO('mysql:host=localhost;dbname=acme_demo;charset=utf8mb4',
        'acme_demo', 'YOUR_PASSWORD',
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
    foreach ($pdo->query('SELECT body FROM notes') as $row) {
        echo htmlspecialchars($row['body']);
    }

    Replace the names and password with yours.

  5. Step 5: Visit it

    Open https://yourdomain.co.uk/dbtest-7h2k.php. You should see Hello from MySQL.

  6. Step 6: Delete the test

    Delete the script (it contains a password), and drop the notes table or the whole test database when finished. Connecting a PHP application to your database

Check it worked

The test page printed the row from your table, then was deleted.

If something goes wrong

What happensWhat to do
Access denied for userUse the full prefixed user and database names. Database connection failed
Unknown databaseDatabase name typo or missing prefix.

This works on any Traxio account. If you’re still choosing a host, PHP and MySQL hosting is free for 30 days, then £0.99 a month.

Popular

Tip: press / to search from any pageSee all results