Jump to content

Need help fetching an array with a static method


eldan88

Recommended Posts

Hey,

 

 I am new to OOP, and I am trying to echo out the username with a static call. However everytime I try i get the following message.

 

Database query failed
The last query that was made was SELECT * FROM users WHERE id=8

 

Below is the static call I am making .

 

$found_user = User::find_by_id(;
echo $found_user['username'];

 

Here is my user class. In my user class i have included my database class. I have instaniated my database class and assigend it to the $database variable

 

<?php
// THe user class preforms all its FIND methods. 
require_once("database.php");
class User {
	
	public function find_all () {
		global $database; 
	$sql = "SELECT * FROM users "; 
	$result_set = $database->query("SELECT * FROM users ");
	return $result_set; 
		}
		
  public function find_by_id($id=0) { // This is the method I am using
	  global $database; 
	   $result_set = $database->query("SELECT * FROM users WHERE id={$id}");
	   $found = $database->fetch_array($result_set);
	   return $found;
	  
  }
	
}

?>

Below is my database class....

 

<?php
require_once("config.php");

class MySQLDatabase {
	
	private $connection;
	public  $last_query;
	
	


function __construct() {
	$this->database_construct();
	
	
}

function database_construct() {
	$this->connection = mysql_connect(DB_SERVER, DB_USER);
	if(!$this->connection) {
	die ("Database connection could not be made");	
	   } else {
		$db_select = mysql_select_db(DB_NAME, $this->connection)  ;
		if(!$db_select) {
		die ("The database could NOT be selected");	
		}
	   }
	
   }
   
function close_connection() {
	if(isset($this->connection)) {
	mysql_close($this->connection);
	unset($this->connection);	
	}
	
}

public function fetch_array($result_set) {
 mysql_fetch_array($result_set);
return 	mysql_fetch_array($result_set);

}

public function num_rows($result_set) {
	mysql_num_rows($result_set);
	return mysql_num_rows; 
	

}

public function insert_id() {
 	return mysql_insert_id($this->connection);
	
}

public function affected_rows() {
	return mysql_affected_rows($this->connection); 
	
}

public function query($sql){ // this function preforms a mysql query
    $this->last_query = $sql;
	$result  = mysql_query($sql, $this->connection);
	$this->confirm_query($result_set);
	if (!$result) {
	die ("Mysql Query Failed"); 
	}
	return $result;
}

private function confirm_query($result_set) {

    if(!$result_set) {
		$output = "Database query failed" . mysql_error() . "<br />"; 
		$output .= "The last query that was made was {$this->last_query}" ;
		die ($output); 
     }
	
  }

} //end of class MySQLDatabase {


$database = new MySQLDatabase();
$db =& $database;

?>

 

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.