Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO not working in GoDaddy Server

Tags:

php

mysql

pdo

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();
}
like image 902
user2684521 Avatar asked Aug 27 '15 18:08

user2684521


2 Answers

Here is a step-by-step procedure to enable PDO extension on Godaddy:

  1. Login to your CPanel

  2. Go to Web Hosting -> Manage for your domain

  3. Click on Select PHP version menu

enter image description here

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

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

enter image description here

like image 178
Mukesh Chapagain Avatar answered Sep 28 '22 05:09

Mukesh Chapagain


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';
like image 43
Maytham Avatar answered Sep 28 '22 05:09

Maytham