How to create a Database with PDO?


I created an SQL database.
DB_name: id1360365_stuff
DB_user: id1360365_stuff
DB_host: localhost
PW: stuff

I found a tutorial on how to connect to a SQL database using PDO:

<?php $servername = "localhost"; $username = "username"; $password = "password"; try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "CREATE DATABASE myDBPDO"; // use exec() because no results are returned $conn->exec($sql); echo "Database created successfully
"; } catch(PDOException $e) { echo $sql . "
" . $e->getMessage(); } $conn = null; ?>

There are four variables I need to get right:

$servername = “localhost”;
$username = “username”;
$password = “password”;
dbname= MyDB

I assume these need to be adjusted: so I did:

$servername = “localhost”;
$username = “id1360365_stuff”;
$password = “stuff”;
dbname= id1360365_stuff

This doesn’t run even inside basic html/body tags.
I have tried what seems to be every possible permutation of the above information, even swapping out 000webhost passwords. Please help!

Do I have CREATE privileges by default?


Can you explain what you are doing?

You can only create databases via on Manage Database page.


I created a database:
DB Name DB User DB Host
id1360365_qwerty id1360365_qwerty localhost

I tried to access the database using a PHP script and it failed (above).
I tried several other PHP scripts and they could not connect to the SQL database.
How to connect my PHP scripts to the SQL database?


Does that help?

You are running these scripts on your 000webhostappurl yes?

You CANNOT run these scripts LOCALLY on your PC for example saved in a file.


Got it, thx so much!