MySQL不会加载到localhost上

时间:2014-12-17 13:40:54

标签: php mysql phpmyadmin

我无法在localhost上加载mysql表。下面我使用PDO,表格工作正常。没有错误...这个表在phpmyAdmin中运行良好...请帮助我疯狂。我从我的Web服务器将表导入到早期版本的本地主机..

<?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();
}

?>

但是当我使用这个代码时它显示一个空白的屏幕,没有错误..我尝试了与其他表相同的代码,它工作正常。

<?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;
?>

我在我的php error.log

中得到这个

[2014年12月17日03:39:55欧洲/柏林] PHP致命错误:未捕获异常&#39; PDOException&#39; with message&#39; SQLSTATE [HY000] [2005] Unknown MySQL server host&#39; localhost:8888&#39; (20)&#39;在/Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10 堆栈跟踪:

0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10):PDO-&gt; __ construct(&#39; mysql:host = loca ...&#39;,&#39; root&#39 ;,&#39; root&#39;)

1 {main}

在第10行的/Applications/MAMP/htdocs/iosLeads/iosCustomerError.php中抛出 [2014年12月17日03:40:00欧洲/柏林] PHP致命错误:未捕获异常&#39; PDOException&#39; with message&#39; SQLSTATE [HY000] [2005] Unknown MySQL server host&#39; localhost:8888&#39; (20)&#39;在/Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10 堆栈跟踪:

0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10):PDO-&gt; __ construct(&#39; mysql:host = loca ...&#39;,&#39; root&#39 ;,&#39; root&#39;)

1 {main}

在第10行的/Applications/MAMP/htdocs/iosLeads/iosCustomerError.php中抛出 [2014年12月17日03:40:23欧洲/柏林] PHP致命错误:未捕获异常&#39; PDOException&#39; with message&#39; SQLSTATE [HY000] [2005] Unknown MySQL server host&#39; localhost:8888&#39; (20)&#39;在/Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10 堆栈跟踪:

0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10):PDO-&gt; __ construct(&#39; mysql:host = loca ...&#39;,&#39; root&#39 ;,&#39; root&#39;)

1 {main}

在第10行的/Applications/MAMP/htdocs/iosLeads/iosCustomerError.php中抛出

1 个答案:

答案 0 :(得分:0)

如果$ result中的数组具有UTF-8编码以外的字符串,则函数json_encode()可能会失败并返回false(请参阅PHP manual json_encode)。要检查这一点,请在使用json_encode()之前转储$ result的值:

var_dump($result);

如果$ result中有一个数组,但json_encode()中的其他返回值不是false,则必须为数据库连接设置正确的字符编码。