eldan88 Posted October 11, 2013 Share Posted October 11, 2013 Hey Guys. I am trying to echo out all the username's from the user's table using object oriented style.. However i am having trouble with this one.Its not echoing out when I run the following code. IT keeps on giving me this error message "Fatal error: Call to undefined method MySQLDatabase::fetch_assoc()" $find_all = User::find_all(); while($user = $database->fetch_assoc()) { echo "User" . $user['username']; } Here is my find all method public static function find_all() { global $database; $result_set = $database->query("SELECT * FROM users"); return $result_set; } And my database query method in the database class public function query($sql) { $this->last_query = $sql; $result = $this->_connection->query($sql); $this->confirm_query($result); return $result; } Here is my full Database class <?php require('config.php'); class MySQLDatabase { private $_connection; public $last_query; private $magic_quotes_active; private $real_escape_string_exists; public function __construct() { $this->_connection = new mysqli('localhost', 'root', 'root', 'sandbox', '8080'); if(!$this->_connection) {die('Connection Error'.$_connection->connect_error ); } else { $db_select = $this->_connection->select_db(DB_NAME); } if(!$db_select) { die("Database Selection Failed" . $this->_connection->error); } $this->magic_quotes_active = get_magic_quotes_gpc(); $this->real_escape_string_exists = function_exists( "mysql_real_escape_string" ); } public function query($sql) { $this->last_query = $sql; $result = $this->_connection->query($sql); $this->confirm_query($result); return $result; } //Database Nuteral Methods private function confirm_query($result){ if(!$result) { $output = "Database query failed: "; $output .= "Last query ". $this->last_query; die($output); } } public function num_rows($result_set) { $this->_connection->num_rows($result_set); } public function insert_id() { //Get the last ID inserted over the database return $this->_connection->insert_id(); } public function affected_rows() { return $this->_connection->affected_rows(); } //Database Netural Methods public function __clone(){} public function escape_value( $value ) { if( $this->real_escape_string_exists) { // PHP v4.3.0 or higher // undo any magic quote effects so mysql_real_escape_string can do the work if( $this->magic_quotes_active ) { $value = stripslashes( $value ); } $value = mysql_real_escape_string( $value ); } else { // before PHP v4.3.0 // if magic quotes aren't already on then add slashes manually if( !$this->magic_quotes_active ) { $value = addslashes( $value ); } // if magic quotes are active, then the slashes already exist } return $value; } public function close_connection(){ if(isset($this->_connection)) { $this->_connection->close(); unset($this->_connection); } } }//end of class $database = new MySQLDatabase(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/282881-need-help-with-oop/ Share on other sites More sharing options...
requinix Posted October 11, 2013 Share Posted October 11, 2013 undefined method MySQLDatabase::fetch_assocYour thoughts on what that might mean? Quote Link to comment https://forums.phpfreaks.com/topic/282881-need-help-with-oop/#findComment-1453511 Share on other sites More sharing options...
NiTx Posted October 11, 2013 Share Posted October 11, 2013 It looks like you've been watching Lynda tutorials? Make sure you don't skip any of the videos and try actually MAKE SENSE of what your learning. The error your getting spells it out for you, the fetch_assoc method doesnt exist.. Plain and simple, you need to add it. I will spoon feed you. Add this to your database class. (I've added extras for your convenience) public function fetch_assoc($result_set) { return mysql_fetch_assoc($result_set); } public function num_rows($result_set) { return mysql_num_rows($result_set); } public function fetch_array($result_set) { return mysql_fetch_array($result_set); } Quote Link to comment https://forums.phpfreaks.com/topic/282881-need-help-with-oop/#findComment-1453520 Share on other sites More sharing options...
trq Posted October 11, 2013 Share Posted October 11, 2013 On a side note. Forget the global keyword exists, it breaks all the encapsulation that functions and objects provide. Quote Link to comment https://forums.phpfreaks.com/topic/282881-need-help-with-oop/#findComment-1453550 Share on other sites More sharing options...
Solution Barand Posted October 11, 2013 Solution Share Posted October 11, 2013 It looks like you've been watching Lynda tutorials? Make sure you don't skip any of the videos and try actually MAKE SENSE of what your learning. The error your getting spells it out for you, the fetch_assoc method doesnt exist.. Plain and simple, you need to add it. I will spoon feed you. Add this to your database class. (I've added extras for your convenience) public function fetch_assoc($result_set) { return mysql_fetch_assoc($result_set); } public function num_rows($result_set) { return mysql_num_rows($result_set); } public function fetch_array($result_set) { return mysql_fetch_array($result_set); } fetch_assoc() does exist, but it is a result object method, not a database object method. $result = $db->query($sqlStr); // returns result object; you can use fetch_assoc with the result object $row = $result->fetch_assoc(); And why would he want to add those deprecated functions of yours when he is using mysqli ? Quote Link to comment https://forums.phpfreaks.com/topic/282881-need-help-with-oop/#findComment-1453557 Share on other sites More sharing options...
ignace Posted October 11, 2013 Share Posted October 11, 2013 (edited) if( $this->real_escape_string_exists) { // PHP v4.3.0 or higherCracks me up when I see this and they use public function. Public, private, protected is available only in PHP5, so stop doing checks for things that were added in PHP4. Edited October 11, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/282881-need-help-with-oop/#findComment-1453558 Share on other sites More sharing options...
mac_gyver Posted October 11, 2013 Share Posted October 11, 2013 this thread is exactly the same issue at the start of your previous thread. which means you are not getting the gist of what is actually being returned/stored in the variables in the code. you are getting an instance of a mysqli result object back from a method/function call, User::find_all(); in this case, and assigning that to a php variable $find_all. in the previous thread, you were directly calling your database class's ->query() method and storing the result object in a php variable $result. so, you have instance of a result object in a variable. to access any properties/methods of that result object, you would use $variable_holding_object->some_property or $variable_holding_object->some_method(); Quote Link to comment https://forums.phpfreaks.com/topic/282881-need-help-with-oop/#findComment-1453563 Share on other sites More sharing options...
eldan88 Posted October 12, 2013 Author Share Posted October 12, 2013 fetch_assoc() does exist, but it is a result object method, not a database object method. $result = $db->query($sqlStr); // returns result object; you can use fetch_assoc with the result object $row = $result->fetch_assoc(); And why would he want to add those deprecated functions of yours when he is using mysqli ? Barnard! I kept on getting confused with using the MySQLDatabase class that I instantiated and the mysqli result object, that I needed to call. I had the same issue in the last thread you responded to as mac_gyver pointed out. But now i fully understand the difference between the to. I have tried what you showed me and it works. Thanks again! Mac_gyver. Thanks for that clear you explanation. Quote Link to comment https://forums.phpfreaks.com/topic/282881-need-help-with-oop/#findComment-1453668 Share on other sites More sharing options...
eldan88 Posted October 12, 2013 Author Share Posted October 12, 2013 On a side note. Forget the global keyword exists, it breaks all the encapsulation that functions and objects provide. What do you mean by this? Quote Link to comment https://forums.phpfreaks.com/topic/282881-need-help-with-oop/#findComment-1453669 Share on other sites More sharing options...
mac_gyver Posted October 12, 2013 Share Posted October 12, 2013 (edited) encapsulation refers to enclosing, putting a shell around, isolating the code so that the only interaction with an instance of the class is though it's public methods/properties. once you have written and tested the code for a class, you should not need to remember (or document) anything about the actual internal implementation, just that it has methods/properties, what those methods accept as inputs, and what values are returned by the methods/properties. in short, you should be able to search through the main code in a program and find all the interaction with any class just by searching for the variable holding the instance of the class. by using the global keyword to bring a variable into the class, this isolation, general purpose nature, is broken. you must now remember that any one class needs you to set up an external variable and what the names are of all the global variable's for all the different classes that are using global variables in. you have also tied the class to using a specific external variable name, so it's not possible for you to us that variable name for anything else in the main code or for you to make additional instances of a class that needs a different value in a global variable. an example. your class is apparently a user class. what happens when you need two (or more) instances of your user class (you are writing a user administration page where one instance is the admin, another instance is the user he is editing), each with a different database or requiring a different database connection? using the global keyword creates a bunch of more work to accomplish this. however, if the code is written without using the global keyword, all you need to do is create another instance of your database class for the different database/connection, i.e. $db2, then create another instance of your user class, where you pass the database connection into the user class either in the constructor or through a specific setter method. Edited October 12, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/282881-need-help-with-oop/#findComment-1453671 Share on other sites More sharing options...
KevinM1 Posted October 12, 2013 Share Posted October 12, 2013 In short, never, ever, ever use 'global' for anything, regardless if you're writing OO or procedural code. And if you're using a resource (book, tutorial, video, etc.) that has 'global' in its code, consider that resource suspect. 'global' is a sign of doing it wrong. Quote Link to comment https://forums.phpfreaks.com/topic/282881-need-help-with-oop/#findComment-1453676 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.