1

我正在使用这个数据库 MySQLi 包装器:

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;
    }
}

但是当我进行查询时,例如 SELECT This FROM Database,并且我想显示结果,我必须回显 $result[0]['This']。为什么?为什么不使用 $result['This']?我改变了这个:

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

对此:

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

但是为什么我看到的所有包装器都返回一个多维数组?

希望你能理解我。谢谢!

4

3 回答 3

1

它们返回多维数组的原因是您可以有多个结果。

所以:

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

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

您可以创建一个fetchRow()函数,当您只期望一行时,它总是只返回第一条记录

return $data[0]

或者

一个 fetchOne()

return $data[0]['name']

当您只期望一个字段时

于 2013-07-22T15:19:40.640 回答
1

通常,当您从数据库查询中收集结果时,您会有多行。

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

所以你得到一个多维数组来处理一个对象中的所有行。

……这里不是这样吗?

编辑:

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

在上面的代码中,您将每个单独的行分配给 $results 数组中的索引。为了访问它们,您需要提供 $results[ROW_NUMBER][KEY_NAME];

那有意义吗?

于 2013-07-22T15:18:19.277 回答
0

我要试一试,说也许试试 $query->fetch_row() 而不是 $query->fetch() (只是猜测)

于 2013-07-22T15:21:08.417 回答