我无法在本地主机上加载mysql表。下面我使用了PDO,而且桌子工作得很好。没有errors...the表在phpmyAdmin...please中工作得很好,帮助我发疯。我从我的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里找到了这个
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
发布于 2014-12-17 14:19:22
如果$result中的数组具有非UTF-8编码的字符串,则函数json_encode()可能会失败并返回false (参见编码,编码)。要检查这一点,在使用$result ()之前转储json_encode的值:
var_dump($result);如果$result中有一个数组,但没有false ()返回值,则必须为数据库连接设置正确的字符编码。
https://stackoverflow.com/questions/27526905
复制相似问题