我正在开发一个网站,它在xammp和mysql上可以在本地正常运行,但是当我尝试使其连接到远程mysql服务器时,它将引发此错误:
致命错误:未捕获错误:在bool上调用成员函数fetchAll()
远程连接数据库时不会抛出错误,但是我没有得到任何数据。
class Database
{
public $DB_SERVER;
public $DB_USER;
public $DB_PASSWORD;
public $DB_DATABASE;
public function __construct()
{
# set the connection variables
$this->DB_SERVER = ''; // contents removed for post
$this->DB_USER = '';
$this->DB_PASSWORD = '';
$this->DB_DATABASE = '';
}
public function getConnection()
{
$dataSourceName = 'mysql:dbname='.$this->DB_DATABASE.';host='.$this->DB_SERVER;
$dbConnection = null;
try
{
$dbConnection = new PDO($dataSourceName, $this->DB_USER, $this->DB_PASSWORD);
}
catch(PDOExecption $err)
{
echo 'Connection failed: ', $err->getMessage();
}
return $dbConnection;
}
public function vw_productsForSale()
{
$connection = $this->getConnection();
$sql = "SELECT * FROM vw_productsForSale";
$statement = $connection->query($sql);
try{
$rowSet = $statement->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOExecption $err)
{
echo 'Connection failed: ', $err->getMessage();
}
$statement = null;
$connection = null;
return $rowSet;
}