0
votes

I can't get mysql table to load on localhost. below i used PDO and the table worked fine. no errors...the table works fine in phpmyAdmin...please help driving me nuts. i imported table from my web server to local host from a earlier version..

<?php

$user = "root";
$pass = "root";

try {
     $dbh = new PDO('mysql:host=localhost;dbname=iosLeads',$user,$pass);
     foreach($dbh->query('SELECT * from Customer') as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
  print "Error! :" . $e->getMessage()."<br/>";
  die();
}

?>

but when i used this code it show a blank screen, no errors..i tried same code with other tables and it works fine.

<?php

        // set up the connection variables
        $db_name  = 'iosLeads';
        $hostname = 'localhost';
        $username = 'root';
        $password = 'root';

        // connect to the database
        $dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);

        // a query get all the records from the users table
        $sql = 'SELECT * FROM Customer';

        // use prepared statements, even if not strictly required is good practice
        $stmt = $dbh->prepare( $sql );

        // execute the query
        $stmt->execute();

        // fetch the results into an array
        $result = $stmt->fetchAll( PDO::FETCH_ASSOC );

        // convert to json
        $json = json_encode( $result );

        // echo the json string
        echo $json;
?>

I'm getting this in my php error.log

[17-Dec-2014 03:39:55 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10 Stack trace:

0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')

1 {main}

thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10 [17-Dec-2014 03:40:00 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10 Stack trace:

0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')

1 {main}

thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10 [17-Dec-2014 03:40:23 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10 Stack trace:

0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')

1 {main}

thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10

1
Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); In addition add error reporting to your PDO statements. - Jay Blanchard
Both codes are correct, I feel issue is something else - Ram Sharma
The code does look fine, adding error reporting can help us get to the problem @RamSharma - Jay Blanchard
Yes, you are right it would help to find out the issue - Ram Sharma
@peter Balsamo, just try to check that table name is Customer or customer. - Ram Sharma

1 Answers

0
votes

If the array in $result has strings other than UTF-8 encoded, the function json_encode() may fail and will return false (see PHP manual json_encode). To check this, dump the value of $result before using json_encode():

var_dump($result);

If you have an array in $result but no other return value than false from json_encode(), you must set the correct character encoding for the database connection.