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.

TutorialBeginner15 min to completeLast updated
On this page
  1. What you’ll need
  2. Step 1: Create the database and user
  3. Step 2: Open phpMyAdmin
  4. Step 3: Create a table
  5. Step 4: Create a test script
  6. Step 5: Visit it
  7. Step 6: Delete the test
  8. Check it worked
  9. If something goes wrong
Level
Beginner
Time
About 15 min
Steps
6
You’ll finish with
A working result, checked

6 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.

Step 1: Create the database and user

Open Account Manager › 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

Step 2: Open phpMyAdmin

Open Extra Features › phpMyAdmin and select your new database.

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');

Step 4: Create a test script

In System Info & Files › 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.

Step 5: Visit it

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

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 £0.99 a month.

Where this fits: DirectAdmin hosting explains the control panel every Traxio account uses, with a tour and tutorials.

Spotted something out of date or unclear? Tell us and we’ll fix it.

Keep going