可能重复:
作用域错误-在非对象上调用成员函数prepare((
我大约两三个月前在我的windows PC上写了这段代码。它运行。。现在我在Mac上下载了xamplep,并得到以下错误:
致命错误:在/Applications/XAMPP/examplepfiles/htdocs/connect/includs/functions.php的第39行对非对象调用成员函数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;
}
也将对象传递给您的函数,如
function renderTimeline($pdo){
并且在你的代码中
$pdo = new pdo();
renderTimeline($pdo);