Jump to content

Need help fetching an array with a static method


eldan88
Go to solution Solved by 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;

?>

 

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.