Jump to content

Need help using the mysqli fetch array


eldan88

Recommended Posts

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'];

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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' => null
public 'field_count' => null
public 'lengths' => null
public 'num_rows' => null
public 'type' => null

Link to comment
Share on other sites

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' => null
public 'field_count' => null
public 'lengths' => null
public 'num_rows' => null
public 'type' => null

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.