我收到一个错误: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/content/........
虽然如果我在一个文件中执行它,它工作正常!
我的 php 文件:
<?php
require 'includes/dbc.php';
$dbc = new dbc();
$db = $dbc->openDb();
$stmt = $dbc->getAllUsers($db);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['iduser'];
}
?>
我的数据库类:
<?php
class dbc {
public $dbserver = '';
public $dbusername = '';
public $dbpassword = '';
public $dbname = '';
function openDb() {
$db = new PDO('mysql:host=' . $dbserver . ';dbname=' . $dbname . ';charset=utf8', '' . $dbusername . '', '' . $dbpassword . '');
return $db;
}
function getAllUsers($db) {
$stmt = $db->query("SELECT * FROM user");
return $stmt;
}
}
?>