0

What am I doing wrong here folks?

<?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';
?>

Equals to:
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>

tereško
  • 58,060
  • 25
  • 98
  • 150
  • $db isn't defined as an object, thus PHP doesn't like it. It seems your header.php file doesn't contain the appropriates information about $db. – tomzx Jul 11 '09 at 02:41
  • Do you actually have any sort of db object, or are you just looking for `mysql_real_escape_string()`, `mysql_query()` and `mysql_fetch_assoc()`? – deceze Jul 11 '09 at 03:14

2 Answers2

2

You do not have a variable $db, or $db is not the database object you expect. You either need to create it first, or it should have been created in header.php but wasn't.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
-1

In your line

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

$db is supposed to be an object, but apparently it is either nothing or something that's no an object. You need to instantiate (create) the object at some point.

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

Did you do that?

deceze
  • 510,633
  • 85
  • 743
  • 889