PDA

View Full Version : Using $this when not in object contex



Kev0121
03-28-2009, 01:38 AM
Getting this error



Fatal error: Using $this when not in object context in C:\wamp\www\register_oop\index.php on line 7


here is my code



<?php
error_reporting(E_ALL);

require_once "database.php";

$sql = "SELECT * FROM `users` WHERE `id` = 1";
$res = $this->query($sql);
$row = $this->fetch_array($res);

echo $row['username'];

?>

steelaz
03-28-2009, 01:49 AM
You need to create new database object first. Don't know what your database class is called, but assuming it's called "database":



<?php
error_reporting(E_ALL);

require_once "database.php";

$db = new database();

$sql = "SELECT * FROM `users` WHERE `id` = 1";
$res = $db->query($sql);
$row = $db->fetch_array($res);

echo $row['username'];
?>

timgolding
03-28-2009, 03:38 AM
The class name is most probably in that database.php file. Look for the line with


class


On it and it will be the word after that