PDO does not work on GoDaddy server

I am working on a project using a private GoDaddy server. It looks like the project is using mysql_connect to connect to db.

I worked with PDO before and decided that I just created a new file on the server so that I could connect using PDO. However, I cannot get it to work, and I cannot get any errors to show that nothing happens when I run this code.

If I try to select a line after this code block, the line will not be displayed; if I exit the line before this code block, the line will appear.

If I try to execute a prepared statement, nothing will happen. PW, UN, HOST and db name are 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(); } 
+6
source share
3 answers

Some host providers or even your own server may disable or disable the default PDO. GoDaddy is one such provider.

I often see people struggling in such situations, thinking that their PDO code is wrong, and asking the same question.

Therefore, always check the availability of the PDO driver before checking the PDO code. One quick check is to make a call to phpinfo(); That unloads a (long) table of installed components.

Even if you have PDO installed, it guarantees and ensures that the PDO driver is installed and running.

Add the PDO driver verification code before the PDO code, saving you from unnecessary debugging time:

 if (!defined('PDO::ATTR_DRIVER_NAME')) echo 'PDO driver unavailable'; 
+4
source

The following is a step-by-step procedure for enabling the PDO extension in Godaddy:

  • Log in to your CPanel

  • Go to Web Hosting -> Manage for your domain

  • Click Choose PHP Menu

enter image description here

  1. Now you have the opportunity to select the version of PHP from the drop-down list. Select a different version than the one selected (for example, 5.4 or 5.5), and then click Set as current .

  2. After that, check the PHP extension checkboxes on your site (for example, PDO), and then click the Save button located at the bottom.

enter image description here

+4
source

I have the same situation as what you described. After a lot of misunderstanding and debugging, and I figured out the reason / solution. But still not sure why Godaddy PDO has this problem.

PHP v5.4.45

 <?php $pdo = new PDO('mysql:localhost;dbname=mydb', $user, $pass); $sth = $pdo->prepare('select * from tab limit 1'); $sth->execute(); $row = $sth->fetchAll(); echo '<pre>select * from test -- not working -- '; print_r($row); $sth = $pdo->prepare('select now()'); $sth->execute(); $row = $sth->fetchAll(PDO::FETCH_ASSOC); echo "\nselect now -- working -- "; print_r($row); $sth = $pdo->prepare('show databases'); $sth->execute(); $row = $sth->fetchAll(PDO::FETCH_ASSOC); echo "\nshow databases -- working -- "; print_r($row); $sth = $pdo->prepare('show tables'); $sth->execute(); $row = $sth->fetchAll(PDO::FETCH_ASSOC); echo "\nshow tables -- not working -- "; print_r($row); $sth = $pdo->prepare('show privileges'); $sth->execute(); $row = $sth->fetchAll(PDO::FETCH_ASSOC); echo "\nshow privileges -- working but not right -- "; print_r($row); $sth = $pdo->prepare('select * from information_schema.TABLES where TABLE_SCHEMA != \'information_schema\''); $sth->execute(); $row = $sth->fetchAll(PDO::FETCH_ASSOC); echo "\nselect * from information_schema.TABLES -- working -- "; print_r($row); 

No mistake - how confusing. And I check this, it works:

 <?php $pdo = new PDO('mysql:localhost;dbname=mydb', $user, $pass); $sth = $pdo->prepare('select * from mydb.tab limit 1'); $sth->execute(); $row = $sth->fetchAll(); echo '<pre>select * from test -- working !!! -- '; print_r($row); 

And now I came up with this solution, but not sure if this is a Godaddy PDO mistake?

 <?php $pdo = new PDO('mysql:localhost', $user, $pass); $sth = $pdo->prepare('use mydb'); $sth->execute(); $sth = $pdo->prepare('select * from tab limit 1'); $sth->execute(); $row = $sth->fetchAll(); echo '<pre>select * from test -- working !!! -- '; print_r($row); 

Output:

It seems that Godaddy PDO does not use dbname, you need to manually run " use dbname " after the new PDO and before any other SQL

0
source

All Articles