首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MySQL不会加载本地主机

MySQL不会加载本地主机
EN

Stack Overflow用户
提问于 2014-12-17 13:40:54
回答 1查看 1.2K关注 0票数 0

我无法在本地主机上加载mysql表。下面我使用了PDO,而且桌子工作得很好。没有errors...the表在phpmyAdmin...please中工作得很好,帮助我发疯。我从我的web服务器将表从早期版本导入到本地主机。

代码语言:javascript
运行
复制
<?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();
}

?>

但是当我使用这段代码时,它显示的是一个空白屏幕,没有错误.我在其他表中尝试了相同的代码,它运行得很好。

代码语言:javascript
运行
复制
<?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里找到了这个

17-2014年12月03:39:55欧洲/柏林PHP致命错误: /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10堆栈跟踪中带有“SQLSTATEHY000未知MySQL服务器主机'localhost:8888‘(20)’的未命名异常'PDOException‘:

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

1 {main}

抛入第10行的/Applications/MAMP/htdocs/iosLeads/iosCustomerError.php中--2014年12月17日03:40:00欧洲/柏林PHP致命错误: /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10堆栈跟踪中带有“SQLSTATEHY000未知MySQL服务器主机‘localhost:8888 (20)’的未命名异常'PDOException‘:

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

1 {main}

抛入第10行的/Applications/MAMP/htdocs/iosLeads/iosCustomerError.php中--2014年12月17日03:40:23欧洲/柏林PHP致命错误: /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10堆栈跟踪中带有“SQLSTATEHY000未知MySQL服务器主机'localhost:8888‘(20)’消息的未命名异常'PDOException‘:

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

1 {main}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-17 14:19:22

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

代码语言:javascript
运行
复制
var_dump($result);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27526905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档