eldan88 Posted October 6, 2013 Share Posted October 6, 2013 Hey guys, I am trying to do a fetch_array in mysqli(object oriented way). I have assigned instantiated the class and assigned it the private attribute called $_connection. However when I try to call $_connection->fetch_array($result) outside my DB class it won't let me call it since its a private method. I know I could change the attribute to public. But will that be safe? Below is my database class <?php require('config.php'); class MySQLDatabase { private $_connection; 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); } } public function query($sql) { $result = $this->_connection->query($sql); $this->confirm_query($result); return $result; } private function confirm_query($result){ if(!$result) { die("Query Failed" . $this->_connection->error); } } public function __clone(){} public function mysql_prep( $value ) { $magic_quotes_active = get_magic_quotes_gpc(); $new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0 if( $new_enough_php ) { // PHP v4.3.0 or higher // undo any magic quote effects so mysql_real_escape_string can do the work if( $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( !$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(); ?> Below is an example of how I am using the fetch_array $query = "SELECT * FROM users WHERE id = 2"; $result = $database->query($query); $found_user = $database->_connection->fetch_array($result); echo $found_user['username']; Quote Link to comment Share on other sites More sharing options...
Barand Posted October 6, 2013 Share Posted October 6, 2013 try $found_user = $result->fetch_array() fetch_xxxx() are mysqli_result object methods Quote Link to comment Share on other sites More sharing options...
eldan88 Posted October 7, 2013 Author Share Posted October 7, 2013 try $found_user = $result->fetch_array() fetch_xxxx() are mysqli_result object methods Hey I have tried doing that but PHP gave me the following error message.. Warning: mysqli_result::fetch_array() expects parameter 1 to be long, Quote Link to comment Share on other sites More sharing options...
Barand Posted October 7, 2013 Share Posted October 7, 2013 Are you sure that is what you tried? The parameter is optional and, if present, should be one of MYSQLI_NUM, MYSQLI_ASSOC or MYSQLI_BOTH(default). Have you left the $result as a parameter? Quote Link to comment Share on other sites More sharing options...
eldan88 Posted October 7, 2013 Author Share Posted October 7, 2013 I'm not sure what you mean by this. I did dump a var_dump on $result and it returns me an object?? object(mysqli_result)[3]public 'current_field' => nullpublic 'field_count' => nullpublic 'lengths' => nullpublic 'num_rows' => nullpublic 'type' => null Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted October 7, 2013 Share Posted October 7, 2013 Try what Barand said to pass mysqli_assoc option: $found_user = $result->fetch_array(MYSQLI_ASSOC) OR $found_user = $result->fetch_assoc() Quote Link to comment Share on other sites More sharing options...
eldan88 Posted October 7, 2013 Author Share Posted October 7, 2013 jazzman. Passing fetch_assoc doesn't make any sense. Why does the var_dump of $result bring back this object? object(mysqli_result)[3]public 'current_field' => nullpublic 'field_count' => nullpublic 'lengths' => nullpublic 'num_rows' => nullpublic 'type' => null Quote Link to comment Share on other sites More sharing options...
eldan88 Posted October 7, 2013 Author Share Posted October 7, 2013 Sorry! Disregard my last comment! That worked. I have a question though. Why in a procedural code you need to pass in the $result. And in the object oriented way we didn't need to pass in the $result? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 7, 2013 Share Posted October 7, 2013 In the object mode you are calling a method of the result object itself so you don't have to tell it what the object is. In procedural mode you have to pass the result object to the function Quote Link to comment Share on other sites More sharing options...
eldan88 Posted October 9, 2013 Author Share Posted October 9, 2013 Okay gotchya! Thanks Quote Link to comment 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.