eldan88 Posted March 25, 2013 Share Posted March 25, 2013 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/276148-need-help-fetching-an-array-with-a-static-method/ Share on other sites More sharing options...
awjudd Posted March 25, 2013 Share Posted March 25, 2013 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. ~awjudd Quote Link to comment https://forums.phpfreaks.com/topic/276148-need-help-fetching-an-array-with-a-static-method/#findComment-1421015 Share on other sites More sharing options...
jazzman1 Posted March 26, 2013 Share Posted March 26, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/276148-need-help-fetching-an-array-with-a-static-method/#findComment-1421062 Share on other sites More sharing options...
Solution mac_gyver Posted March 26, 2013 Solution Share Posted March 26, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/276148-need-help-fetching-an-array-with-a-static-method/#findComment-1421065 Share on other sites More sharing options...
eldan88 Posted March 26, 2013 Author Share Posted March 26, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/276148-need-help-fetching-an-array-with-a-static-method/#findComment-1421066 Share on other sites More sharing options...
trq Posted March 26, 2013 Share Posted March 26, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/276148-need-help-fetching-an-array-with-a-static-method/#findComment-1421111 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.