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
source share