0

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

Community
  • 1
  • 1
Peter Balsamo
  • 17
  • 1
  • 7

1 Answers1

0

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.

Henrik
  • 2,771
  • 1
  • 23
  • 33
  • Henrik how do i go about this "you must set the correct character encoding for the database connection". – Peter Balsamo Dec 17 '14 at 17:23
  • @PeterBalsamo See this [answer](http://stackoverflow.com/a/4361485/4137828) for how to set the character encoding. – Henrik Dec 18 '14 at 17:31