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
Share on other sites

We would need to see more code. Generally though. Static methods get over used by people who don't know what they are doing / how they work.

 

A database class really needs to be instantiated because it should store at least a little state.

Link to comment
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
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
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
Share on other sites

Could I use a global?

 

That would break any encapsulation you may have. Your design is pretty floored anyways though.

 

Ideally, you would pass your database object (ensuring it implements some interface) to the construct of the userClass.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.