I am working on a project that is using a GoDaddy private server. Looks like the project is using mysql_connect
to connect to the db.
I have worked with PDO before and figured I would just create a new file within the server so I can connect by using PDO. However I can't get it to work, and I can't get any errors to show up, nothing seems to happen when I run this code.
If I try to echo out a string after that block of code, the string wont show up, if I echo out a string before this block of code, the string will show up.
If I try to execute a prepared statement nothing happens. PW, UN, HOST, and db name are all correct. Am I doing something wrong?
<?php
error_reporting(E_ALL);
$dns = "mysql:host=localhost;dbname=mainsql;";
$username = "bowski";
$passwd = "kingsman1";
try {
$db = new PDO($dsn, $username, $passwd);
} catch (PDOException $ex) {
echo $ex->getMessage();
}
Here is a step-by-step procedure to enable PDO extension on Godaddy:
Login to your CPanel
Go to Web Hosting -> Manage
for your domain
Click on Select PHP version menu
Now, you have the option to choose PHP version from the dropdown. Choose a different version than selected (e.g. 5.4 or 5.5) and then click Set as current.
After that, check the checkboxes of PHP extension you want on your site (e.g. PDO) and then click Save button present at the bottom.
Some host providers or even your own server might have PDO disabled or not installed per default. GoDaddy is one such provider.
I often see people struggling in similar situations thinking their PDO code is wrong, and asking the same question.
Therefore, it is always a good practice to check the availability of PDO driver before checking PDO code. One quick check is to perform the phpinfo();
call which dumps a (long) table of installed components.
Even if you have PDO installed, this is to ensure and guaranty that PDO driver is installed and running.
Add PDO driver checker code before your PDO code, saving you from some unnecessary debugging time:
if (!defined('PDO::ATTR_DRIVER_NAME'))
echo 'PDO driver unavailable';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With