OOP致命错误

时间:2009-07-11 02:36:55

标签: php

我在这里做错了什么?

<?php
include 'header.php';

/**
* Display a user's profile
*/

$id = $db->real_escape_string($_GET['id']);
$user_res = $db->query("SELECT * FROM users WHERE id = $id");
$user = $user_res->fetch_assoc();
?>

<h1><?php echo $user['username'] ?>'s Profile</h1>

<?php
include 'footer.php';
?>

相当于:
Fatal error: Call to a member function real_escape_string() on a non-object in C:\wamp\www\test\profile.php on line 12</pre>

2 个答案:

答案 0 :(得分:2)

您没有变量$db,或者$db不是您期望的数据库对象。您需要先创建它,或者它应该在header.php中创建,但不是。

答案 1 :(得分:-1)

在你的行

$db->real_escape_string($_GET['id']);

$db应该是一个对象,但显然它不是任何东西,也不是某个对象。您需要在某个时刻实例化(创建)对象。

$db = new DatabaseObject();
// substitute "DatabaseObject" with the actual name of the Class

你做到了吗?