将行放入数组时,它会重复 [英] When putting rows into array it duplicates

查看:69
本文介绍了将行放入数组时,它会重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为查询的更安全的MySQLi连接创建一个类,但是遇到了这个问题,它像$ [0] => 47, [example_id] => 47这样重复了$ row基本上说我得到了一个这样的表

I am trying to create a class for easier safe MySQLi connections an queries, but I came across this problem where it duplicates $row like [0] => 47, [example_id] => 47 Basically lets say I got a table like this

╒═══════════════════════════════════════════════╕
│                     Content                   |
╞════════════╤═══════════════╤══════════════════╡
│ example_id │ example_title │ example_content  │
╞════════════╪═══════════════╪══════════════════╡
╞════════════╪═══════════════╪══════════════════╡
│ 1          │ Hello World 1 │ Some content 1   │
╞════════════╪═══════════════╪══════════════════╡
│ 2          │ Hello World 2 │ Some content 2   │
╘════════════╧═══════════════╧══════════════════╛


public function select_all(string $table){ // In this case $table = 'Content'
  $this->remove_stmt();

  $this->num_rows = null;
  $this->rows = Array();

  if(!$stmt = $this->con->prepare("SELECT * FROM $table")){
    $this->stmt_is_errors = true;
    $this->stmt_errors = 'Could not prepare statement!';
    return false;
  }

  if(!$stmt->execute()){
    $this->stmt_is_errors = true;
    $this->stmt_errors = 'Could not execute statement!';
    return false;
  }

  if(!$result = $stmt->get_result()){
    $this->stmt_is_errors = true;
    $this->stmt_errors = 'Could not get result content!';
    return false;
  }

  if(!$this->num_rows = $result->num_rows){
    $this->stmt_is_errors = true;
    $this->stmt_errors = 'Could not get number of rows';
    return false;
  }

  $row = Array();
  while($row = $result->fetch_array()){
    $this->rows[] = $row;
    print_r($row);
    echo '<br><br>';
  }

  return true;

}

使用上面示例表中的代码,您将在本文的最上方看到该代码,将输出如下所示的数组:

Using the code above on the example table you can see on the very top of this post will output an array looking like this:

Array(
    [0] => Array(
        [0] => 1,
        [example_id] => 1,
        [1] => "Hello World 1",
        [example_title] => "Hello World 1",
        [2] => "Some content 1",
        [example_content] => "Some content 2"
    ),
    [1] => Array(
        [0] => 2,
        [example_id] => 2,
        [1] => "Hello World 2",
        [example_title] => "Hello World 2",
        [2] => "Some content 2",
        [example_content] => "Some content 2"
    ),
);

我将如何避免这些重复?

How would I avoid these duplicates?

谢谢.理查德.

推荐答案

您正在使用$result->fetch_array(),并且此函数(如果不带参数使用)将使用默认参数MYSQLI_BOTH,该参数同时返回数字数组和每个fetch

You are using $result->fetch_array() and this function, if used without a parameter will use the default parameter which is MYSQLI_BOTH which returns both a numeric array and an assoc array with each fetch

所以要么使用$result->fetch_array(MYSQLI_ASSOC)

或者您可以使用$result->fetch_assoc(),它将仅返回一个assoc数组

or you can use $result->fetch_assoc() which will return just an assoc array

这篇关于将行放入数组时,它会重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆