1

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!

1
  • Although question itself is a quite quite silly one ("I am fetching an array, why should I address it as array"), but +1 for using mysqli wrapper, and a quite smart one. The only objection I have is: like many PHP users you count yourself as a sole user of this script. Commented Jul 22, 2013 at 15:21

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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?

7 Comments

No, when I select multiple fields, for example SELECT X, Y, Z FROM Database I have to echo them with $result[0]['X'] and so on.
Right. Because if you tried to echo $result['X'], you would be trying to refer to a ROW by a name, which it doesn't have. If you want to get just one row, you'll need to use fetch_row, not $fetch.
Where does this $row come from?
Aw, my bad. I beg my pardon. It is coming from that call user func stuff
though the code can be simplified to just while ($query->fetch()) $result[] = $row;
|
0

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

1 Comment

Unfortunately you missed. And whoever upvoted this answer apparently have no experience with mysqli either

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.