# Fetch an array result set using a mysqli wrapper class

I have a PHP class:

<?php
//connectionclass.php
class connectionclass{
public $conn; public$warn;
public $err; function __construct(){$this->connect();
}

private function connect(){
$this->conn = @ new mysqli('localhost', 'sever_user', 'user_password'); if ($this->conn->connect_error) {
$this->conn = FALSE;$this->warn = '<br />Failed to connect database! Please try again later';
}
}

public function get_data($qry){$result = $this->conn->query($qry);
if ($result->num_rows>=1) { while($row=$result->fetch_assoc()){$rows[] = $row; } return$rows;
} else {
$this->err =$this->conn->error;
return FALSE;
}
}

}
?>


and a PHP page:

<?php
include('/include/connectionclass.php');
$db = new connectionclass();$query = "SELECT * FROM USERS WHERE user_country='India'";
$data =$db->get_data($query);$rownum = count($data); for($i=0;$i<$rownum;$i++){ echo$data[$i]['name'].'- '.$data[$i]['age'].'<br />'; } ?>  Is there any idea for an easier way to iterate $data in the PHP page?

First, you can use mysqli_result::fetch_all in your connectionclass::get_data() instead of manually looping and building the result array.

Second, you can use foreach to easily iterate through results

The improvement to your previous question is that you are now successfully encapsulating mysqli, instead of exposing part of it by returning mysqli related objects.

I still stand by my previous answer though: Your function still doesn't make it easier to write queries, and it abstracts away essential functionalities of mysqli (ie prepared statements).

With your function, your code looks like this (I cleaned it up a bit):

$data =$db->get_data("SELECT * FROM users WHERE user_country = 'India'");
for($i = 0;$i < count($data);$i++){
echo $data[$i]['name'] . '- ' . $data[$i]['age'] . '<br />';
}


Without your function, the code would look like this:

$result =$conn->query("SELECT * FROM users WHERE user_country = 'India'");
while($row =$result->fetch_assoc()){
echo $row['name'] . '- ' .$row['age'] . '<br />';
}


If you want to add error handling, each approach would have additional ifs.

So you can see that you are not actually making it any easier to write code, but you are changing the well known mysqli API to your custom function, and you are making it impossible to write secure code, as prepared statements cannot be used with your function.

So instead of your approach, you need to use prepared statements. If you still want to wrap mysqli, try to do it in a way that actually makes writing queries easier. Note though that the mysqli API is already pretty good, so it will not be that easy to improve upon it.

Misc

• write out your variable names. warning and error are not that much more to type, but easier to read.
• Your conception that these types of class could not implement prepared statement is actually not right. It can also implement prepared statement nicely. For your kind information that prepared statement is not always useful. From php.net manual: Using a prepared statement is not always the most efficient way of executing a statement. A prepared statement executed only once causes more client-server round-trips than a non-prepared statement. This is why the SELECT is not run as a prepared statement above. So if you have a shared hosting with limited resources, you should think alternative. Dec 3 '16 at 14:28
• tim, My topics was not for prepared statement, thus it was unnecessary to show in my code, it would complicated my main points. My point in this topic was how to fetch resulting array in an easier way. Xymanek adviced me for usingforeach loop. It Dec 3 '16 at 17:01