Jordy
Jordy

Reputation: 4809

Fetch array, multidimensional?

I'm using this database MySQLi wrapper:

Class dbWrapper {
    protected $_mysqli;
    protected $_debug;

    public function __construct($host, $username, $password, $database, $debug) {
        $this->_mysqli = new mysqli($host, $username, $password, $database);
        $this->_mysqli->set_charset("utf8");
        $this->_debug = (bool) $debug;
        if (mysqli_connect_errno()) {
            if ($this->_debug) {
                echo mysqli_connect_error();
                debug_print_backtrace();
            }
            return false;
        }
        return true;
    }

    public function q($query) {
        if ($query = $this->_mysqli->prepare($query)) {
            if (func_num_args() > 1) {
                $x = func_get_args();
                $args = array_merge(array(func_get_arg(1)),
                    array_slice($x, 2));
                $args_ref = array();
                foreach($args as $k => &$arg) {
                    $args_ref[$k] = &$arg; 
                }
                call_user_func_array(array($query, 'bind_param'), $args_ref);
            }
            $query->execute();

            if ($query->errno) {
              if ($this->_debug) {
                echo mysqli_error($this->_mysqli);
                debug_print_backtrace();
              }
              return false;
            }

            if ($query->affected_rows > -1) {
                return $query->affected_rows;
            }
            $params = array();
            $meta = $query->result_metadata();
            while ($field = $meta->fetch_field()) {
                $params[] = &$row[$field->name];
            }
            call_user_func_array(array($query, 'bind_result'), $params);

            $result = array();
            while ($query->fetch()) {
                $r = array();
                foreach ($row as $key => $val) {
                    $r[$key] = $val;
                }
                $result[] = $r;
            }
            $query->close(); 
            return $result;
        } else {
            if ($this->_debug) {
                echo $this->_mysqli->error;
                debug_print_backtrace();
            }
            return false;
        }
    }

    public function handle() {
        return $this->_mysqli;
    }
}

But when I make a query, for example SELECT This FROM Database, and I want to display the result, I have to echo $result[0]['This']. Why that? Why not $result['This']? I changed this:

$result = array();
    while ($query->fetch()) {
        $r = array();
        foreach ($row as $key => $val) {
            $r[$key] = $val;
        }
        $result[] = $r;
    }
    $query->close(); 
    return $result;

to this:

$result = array();
            while ($query->fetch()) {
                foreach ($row as $key => $val) {
                    $result[$key] = $val;
                }
                $result[] = $r;
            }
            $query->close(); 
            return $result;

But why do all the wrappers I see, return a multidimensional array?

Hope you understand me. Thank you!

Upvotes: 1

Views: 549

Answers (3)

Martin Sheeks
Martin Sheeks

Reputation: 137

Usually, when you're gathering results from a DB query, you have multiple rows.

  --------------
  - 1 --- This -
  - 2 --- That -
  --------------

So you get a multi-dimensional array in order to handle all the rows in one object.

...Is that not the case here?

EDIT:

  $result = array();
      while ($query->fetch()) {
          $r = array();
          foreach ($row as $key => $val) {
              $r[$key] = $val;
          }
          $result[] = $r;
      }
      $query->close(); 
      return $result;

in the above code, you are assigning each individual row to an index in the $results array. In order to access them, you need to provide $results[ROW_NUMBER][KEY_NAME];

Does that make sense?

Upvotes: 1

Tocacar
Tocacar

Reputation: 475

I'm going to take a shot and say maybe try $query->fetch_row() instead of $query->fetch() (just a guess)

Upvotes: 0

We0
We0

Reputation: 1149

The reason they return multidimensional arrays is because you can have more than one result.

So:

$data[0]['name'] is the first returned records name.

$data[1]['name'] is the second returned records name.

You can create a fetchRow() function which will always just return the first record when you only expect one row

return $data[0]

OR

a fetchOne()

return $data[0]['name']

When you only expect one field

Upvotes: 1

Related Questions