Php OOP致命错误
伙计们,我做错了什么Php OOP致命错误,php,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><?p
<?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';
?>
您没有变量$db
,或者$db
不是您期望的数据库对象。您可能需要先创建它,或者它应该在header.php中创建,但不是在您的行中创建的。
$db->real_escape_string($_GET['id']);
$db
应该是一个对象,但显然它要么什么都不是,要么不是对象。您需要在某个点实例化(创建)对象
$db = new DatabaseObject();
// substitute "DatabaseObject" with the actual name of the Class
您这样做了吗?$db没有定义为对象,因此PHP不喜欢它。您的header.php文件似乎不包含有关$db的适当信息。您是否确实有任何类型的db对象,或者您只是在寻找mysql\u real\u escape\u string()
,mysql\u query()
和mysql\u fetch\u assoc()
?