Search code examples
phpmysqlpdoconnection

Cannot connect to Mysql database with HostGator (PHP)


I've been trying to connect to my MYSQL database on my newly acquired website through a PDO connection which is quoted below, and it doesn't seem to be working properly, as it fails as soon as I try using a query or to add an entry. All the information seems to be right, but whenever I try using any command, as selecting or adding entries, it gives me this error:

Array ( [0] => 3D000 [1] => 1046 [2] => No database selected ) 1

Here are the lines of my connection:

try {
    $db = new PDO('mysql:localhost;dbname=username_dbName', 'username_dbUser', 'password');
}
catch (Exception $e) {
        die('Erreur : ' . $e->getMessage());
        print 'Unable to Connect. Please contact the website administrator.';
}

And here is the code I used to test if queries worked.

$req = $db->prepare('INSERT INTO testTable(number) 
                        VALUES(:number)');
$req->execute(array('number' => 13)) or die(print_r($req->errorInfo()));

Obviously I use the actual login values for the connection, but it's not working and I can't figure why. My website is hosted by HostGator, if that can help.

Thanks in advance for your answers!


Solution

  • Try this

    try {
        $db = new PDO('mysql:dbname=username_dbName;host=localhost', 'username_dbUser', 'password');
    }