使用此代码列出表的行时出现错误
// Specify your table name
$table_name = 'table_stats';
$hostname = '172.16.11.2';
$username = 'user_stats';
$password = 'stats';
$dbname = 'db_stats';
$dbh = new PDO("mysql:$hostname;dbname=$dbname",$username,$password);
$stmt = $dbh->query("SELECT * FROM $table_name", PDO::FETCH_ASSOC);
foreach($stmt as $row) { // PDOStatement implement Traversable interface, so you can just loop over the resultset. Sweet!
print_r($row);
}
?>
我得到这个错误
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)' in /home/mahmedne/public_html/android/updates/test.php:12 Stack trace: #0 /home/mahmedne/public_html/android/updates/test.php(12): PDO->__construct('mysql:172.16.25...', 'mahmedne_stats', 'stats') #1 {main} thrown in /home/mahmedne/public_html/android/updates/test.php on line 12
我可以确认数据库设置是 100% 正确的,如果我像连接一样使用相同的值
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
然后它可以工作,但不适用于我做 new PDO()
请告知,我是否有可能提供有限的 PHP 功能?我正在网络主机上检查此代码,而不是在我的本地机器上
这是我使用 phpinfo() 获得的与 PDO 相关的信息
PDO
PDO support enabled
PDO drivers sqlite, sqlite2, mysql
pdo_mysql
PDO Driver for MySQL enabled
Client API version 5.1.65
Directive Local Value Master Value
pdo_mysql.default_socket /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock
pdo_sqlite
PDO Driver for SQLite 3.x enabled
SQLite Library 3.7.7.1
谢谢,