Friday, 15 June 2012

php - mysqli array does not print values properly -



php - mysqli array does not print values properly -

i'm having problem printing out values mysqli query. here db connection class using.

class db { public function __construct() { $this->mysqli = new mysqli('localhost', 'root','', 'database'); if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); } } public function query($sql) { $this->sql = $this->mysqli->real_escape_string($sql); $this->result = $this->mysqli->query($sql); if ($this->result == true) homecoming true; else die('problem query: ' . $this->sql); } public function get($field = null) { if ($field == null) { $data = array(); while ($row = $this->result->fetch_assoc()) { $data[] = $row; } } else { $row = $this->result->fetch_assoc(); $data = $row[$field]; } $this->result->close(); homecoming $data; } public function __destruct() { $this->mysqli->close(); } }

running query

$db = new db; $db->query("select * tblclients clientid = $this->id"); $result = $db->get(); echo $result['clientid'];

i'm getting error

php notice: undefined index: clientid

however know values getting passed $results array when run

print_r ($result);

i returned

array ( [0] => array ( [clientid] => 2 [firstname] => john [lastname] => doe [dob] => 1962-05-08))

for worth, if seek echo $db->get('firstname'); works. been banging head against wall while now, help appreciated.

as can see have array within array. need need go this:

$result[0]['clientid'];

so we're doing here first calling $result variable contains array index of [0], array contains column names query (ex: ['clientid']).

so have go deeper $result['clientid'] info database firstly calling array contains keys database.

to un-nest array, like:

$result = $db->get(); $normal_result = 0; foreach ($result $value) { $normal_result = $value; }

you can utilize within method, you'll normal results in future.

php sql mysqli

No comments:

Post a Comment