可能重复:
Scope error - Call to a member function prepare() on a non-object
我在2个月或3个月前在我的Windows PC上写了这段代码。它运行..现在我在我的Mac上下载了xampp,我收到以下错误:
致命错误:在第39行/Applications/XAMPP/xamppfiles/htdocs/connect/includes/functions.php中的非对象上调用成员函数prepare()
// Connect to the database
try {
$pdo = new PDO('mysql:host=localhost;dbname=mo33', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}catch (PDOException $e){
$error = "There was an issue connecting to the database: \n" + $e->getMessage();
include 'html/error.html.php';
exit();
}
function renderTimeline(){
try {
$sql = 'SELECT user_publications.id, user.firstname, user.lastname, user.thumbnail, article, date_added
FROM user_publications INNER JOIN user
ON user_id = user.id
ORDER BY user_publications.id DESC';
$s = $pdo->prepare($sql); ---------------------------LINE 39---------------
$result = $pdo->query($sql);
}catch(PDOException $e){
$output = 'There was an error while finding posts in the database..: ' . $e->getMessage();
include 'html/error.html.php';
exit();
}
while($row = $result->fetch()) {
$user_publications[] = array(
'id'=>$row['id'],
'name'=>$row['firstname'] + " " + $row['lastname'],
'thumbnail'=>$row['thumbnail'],
'article'=>$row['article'],
'date'=>$row['date_added']);
}
foreach ($user_publications as $post) {
renderUserPublication($post['id'], $post['name'], $post['thumbnail'], $post['article'], $post['date']);
}
return;
}
答案 0 :(得分:0)
将对象传递给你的函数
function renderTimeline($pdo){
并在您的代码中
$pdo = new pdo();
renderTimeline($pdo);