致命错误:在第 7 行的 /.../public_html/deals/includes/deal.php 中的非对象上调用成员函数 prepare()
我已经为交易创建了一个数据库,并插入了所有带有示例数据的表。
index.php(尚未完成)
include_once('includes/connection.php');
include_once('includes/deal.php');
$deal = new Deal;
$deals = $deal->fetch_all();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GameDeals</title>
<link rel="stylesheet" href="assets/deals.css" />
</head>
<body>
<div class="container">
<!-- START GAMEDEALS -->
<?php foreach ($deals as $deal) { ?>
<div>dsdsd<?php echo $deal['deal_title']; ?></div>
<?php } ?>
<!-- END GAMEDEALS -->
</div>
</body>
</html>
连接.php
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=****', $**username**, $**pwd**);
}
catch (PDOException $e)
{
return "Database connection error.";
}
?>
处理.php
<?php
class Deal {
public function fetch_all() {
global $pdo;
$query = $pdo->prepare("SELECT * FROM deals");
$query->execute();
return $query->fetchAll();
}
}
?>
有任何想法吗?
<?php
class Deal {
public function fetch_all() {
global $pdo;
var_dump($pdo);
$query = $pdo->prepare("SELECT * FROM deals");
$query->execute();
return $query->fetchAll();
}
}
?>
结果:
NULL 致命错误:在第 8 行的 /.../public_html/deals/includes/deal.php 中的非对象上调用成员函数 prepare()