Jump to content

Error


unidox

Recommended Posts

I have just started picking up PHP again, so my OOP skills are a bit rusty. I keep getting this error:

 

Fatal error: Using $this when not in object context in C:\wamp\www\core\dbClass.php on line 93

 

That line is:

 

$this->last_query = "Table: " . $table . "Query: " . $sql;

 

I am calling an sql function inside dbClass from userClass:

 

$query = dbClass::sql ( "users", "WHERE `id` = '$userId'" );

 

Any ideas what I am doing wrong? Thanks

Link to comment
https://forums.phpfreaks.com/topic/207777-error/
Share on other sites

dbClass:

<?php



class dbClass {

private $last_error;

private $last_query;

private $row_count;



public $owner_email;



public $db_host;

public $db_username;

public $db_password;

public $db_table;

public $site_dir;



public $prefix;



private $link;



function __construct( $host, $username, $password, $table ) {

	$this->db_host = $host;

	$this->db_username = $username;

	$this->db_password = $password;

	$this->db_table = $table;

	$this->connect();

}

function connect () {

	$this->link = mysql_connect($this->db_host, $this->db_username, $this->db_password);



	if (!$this->link) {

		$this->last_error = mysql_error();

		return false;

	}

	if (!$this->select_table()) {

		return false;

	}

	return $this->link;

}

function select_table() {

	if (!mysql_select_db($this->db_table)) {

		$this->last_error = mysql_error();

		return false;

	}

	return true;

}

function sql($table, $sql) {

	$this->last_query = "Table: " . $table . "Query: " . $sql;

	$row = mysql_query("SELECT * FROM `" . $prefix . $table . "` " . $sql . "") or die ( mysql_error ( ) );

	if (!$row) {

		$this->last_error = mysql_error();

		return false;

	}

	$this->row_count = mysql_num_rows($row);

	return $row;

}

function get_row($result, $type="MYSQL_BOTH") {

	if (!$result) {

		$this->last_error = "Invalid resource identifier passed to get_row() function.";

		return false;

	}

	if ($type == 'MYSQL_ASSOC') $row = mysql_fetch_array($result, MYSQL_ASSOC);

	if ($type == 'MYSQL_NUM') $row = mysql_fetch_array($result, MYSQL_NUM);

	if ($type == 'MYSQL_BOTH') $row = mysql_fetch_array($result, MYSQL_BOTH); 

	if (!$row) return false;

	return $row;

}

function insert_sql($table, $sql1, $sql2) {

	$this->last_query = "Table: " . $table . "Query: " . $sql1 . " | " . $sql2;

	$row = mysql_query("INSERT INTO `" . $this->prefix . $table . "` (" . $sql1 . ") VALUES (" . $sql2 . ")") or die(mysql_error());

	if (!$row) {

		$this->last_error = mysql_error();

		return false;

	}

	return true;

}

function update_sql($table, $sql) {

	$this->last_query = "Table: " . $table . "Query: " . $sql;

	$row = mysql_query("UPDATE `" . $this->prefix . $table . "` " . $sql . "");

	if (!$row) {

		$this->last_error = mysql_error();

		return false;

	}

	return true;

}

function escape_sql($data) {

	return mysql_real_escape_string($data);

}

function print_last_error() {

	return $this->last_error;

}

function print_last_query() {

	return $this->last_query;

}

}



?>

 

Function being used inside userClass:

public function getData ( $userId, $rowName ) {

        $query = dbClass::sql ( "users", "WHERE `id` = '$userId'" );

        if ( mysql_num_rows( $query ) == 1 ) {

            $row = dbClass::get_row( $query );

        } else {

            return "Error: No rows in query.";

        }

        return $row[$rowName];

    }

 

 

Link to comment
https://forums.phpfreaks.com/topic/207777-error/#findComment-1086169
Share on other sites

Pfff. You haven't even declared any methods within the dbClass as static.

 

This....

 

$query = dbClass::sql ( "users", "WHERE `id` = '$userId'" );

 

should be....

 

$db = new dbClass($host, $username, $password, $table);
$query = $db->sql("users", "WHERE `id` = '$userId'" );

 

ps: The $table variable is misleading, it should be $dbname or similar.

Link to comment
https://forums.phpfreaks.com/topic/207777-error/#findComment-1086172
Share on other sites

Well, the db is already declared inside index.php:

<?php
session_start( );

// exit ( $_SESSION['userId'] );

include ("../config.php");

include ("../core/coreClass.php");
include ("../core/dbClass.php");
include ("../core/userClass.php");

define (IN_SCRIPT, true);

$coreClass = new coreClass ( );
$dbClass = new dbClass ( $host, $username, $password, $table );
$userClass = new userClass ( );

if ( !$userClass->checkLoggedIn( ) ) {
    $currentPage = "login";
} elseif ( $coreClass->getGET("page") && file_exists ( "pages/".$coreClass->getGET("page").".php" ) ) {
    $currentPage = $coreClass->getGET("page");
} else {
    $currentPage = "home";
}
include ( "processors/".$currentPage.".php" );
include ( "pages/".$currentPage.".php" );
?>

 

Link to comment
https://forums.phpfreaks.com/topic/207777-error/#findComment-1086174
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.