vendredi 27 mars 2015

How do I connect to a database I created in HostGator?

Edit: I have googled and searched, read what I could find of their documentation. Even chatted with them. The chatter could help me. This is why I reach to you.


I am a complete beginner and I am having some troubles getting started with databases on hostgator. I guess my question also is valid using other hosts.


I created a db through the cpanel in hostgator and added a user to it.


I copied this script into a test.php in my /public_html/ folder and ran it on my site.


In the script I used the name, user and password from the database and user I previously created in cpanel. This database I can see using phpMyAdmin.



<?php
try
{
//open the database
$db = new PDO('sqlite:localhost;dbname=user_db', 'user_username', 'password');

//create the database
$db->exec("CREATE TABLE Dogs (Id INTEGER PRIMARY KEY, Breed TEXT, Name TEXT, Age INTEGER)");

//insert some data...
$db->exec("INSERT INTO Dogs (Breed, Name, Age) VALUES ('Labrador', 'Tank', 2);".
"INSERT INTO Dogs (Breed, Name, Age) VALUES ('Husky', 'Glacier', 7); " .
"INSERT INTO Dogs (Breed, Name, Age) VALUES ('Golden-Doodle', 'Ellie', 4);");

//now output the data to a simple html table...
print "<table border=1>";
print "<tr><td>Id</td><td>Breed</td><td>Name</td><td>Age</td></tr>";
$result = $db->query('SELECT * FROM Dogs');
foreach($result as $row)
{
print "<tr><td>".$row['Id']."</td>";
print "<td>".$row['Breed']."</td>";
print "<td>".$row['Name']."</td>";
print "<td>".$row['Age']."</td></tr>";
}
print "</table>";

// close the database connection
$db = NULL;
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
?>


This worked, which is nice, but it created a file in my /public_html/ folder called 'localhost;dbname=user_db'


My issue is that I thought I was connecting to the database I created using cpanel, but when I open phpMyAdmin, that database is empty.


How do I change that script to talk to the database I created using cpanel so that I can reach it using phpMyAdmin?


Aucun commentaire:

Enregistrer un commentaire