Jump to content

Need help fetching an array with a static method


Go to solution Solved by mac_gyver,

Recommended Posts

Hey,

 

 I am new to OOP, and I am trying to echo out the username with a static call. When i tried to get the call, I get nothing displayed on my browser. Not even an error message?

 

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) {
	  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($sql);
	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;

?>

 

You are statically calling the find_by_id function but it isn't a static function (i.e. you need an instance to do that).

 

Change it from public function to public static function.

 

Every public method in the class could be calling without instantiation.

 

Example:

 

<?php

class Area {
    
    public function calculate_area($length, $bredth) {
     return $length * $bredth;   
    }
}

$result=Area::calculate_area(225, 75);

echo "The area is: $result";

Results:

 

 

Notice: Strict standards: Non-static method Area::calculate_area() should not be called statically in /var/www/html/test/debug.php on line 10

 

The area is: 16875

 

  • Solution

mysql_fetch_array($result_set);

return     mysql_fetch_array($result_set);

 

your function code is fetching a row from the result set, but not doing anything with it, then trying to fetch and return the next row, but since there's likely only one matching row from the query in question, you are actually returning a false value to the calling code.

your function code is fetching a row from the result set, but not doing anything with it, then trying to fetch and return the next row, but since there's likely only one matching row from the query in question, you are actually returning a false value to the calling code.

Wow you right. I changed my function to public function from 

 

fetch_array($result_set) {

mysql_fetch_array($result_set);

return     mysql_fetch_array($result_set);

 

}

 

 to fetch_array($result_set) {

return     mysql_fetch_array($result_set);

 

}

 

 and it solved the issue. Thank you!

Sorry, but whatever resource your learning "OOP" from should be thrown out the window.

 

Globals completely break the capsulation that classes are designed to provide.

 

Static methods also make it very easy to do the same.

 

If you want to get started with OOP you should at least follow some decent guidelines. Dependency inject is a very simple concept and looking at your code, is something you really need to be aware of at this point in time.

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.