MySQLI _ FETCH _ array() Return Value Array Size Problem?

  mysql, question

For ease of use, I simply packaged the mysqli method:

<?  Php
 
 class DatabaseAccesser {
 var $host;
 var $username;
 var $password;
 var $link;
 var $resultSet;
 
 function __construct()
 bracket
 $this->host = "localhost";
 $this->username = "hands";
 $this->password = "hands";
 bracket
 
 function __destruct()
 bracket
 mysqli_close($this->link);
 bracket
 
 function connect()
 bracket
 $this->link = mysqli_connect($this->host, $this->username, $this->password);
 if(!  $this->link)
 bracket
 echo mysqli_connect_error();
 bracket
 bracket
 
 function select_datebase($database)
 bracket
 mysqli_select_db($this->link, $database);
 bracket
 
 function execute($sql)
 bracket
 $this->resultSet = mysqli_query($this->link, $sql);
 return $this->resultSet;
 bracket
 
 function getResultSet()
 bracket
 return $this->resultSet;
 bracket
 
 function fetch_result()
 bracket
 $row = mysqli_fetch_array($this->resultSet);
 return $row;
 bracket
 
 bracket
 
 
 $test = new DatabaseAccesser();
 $test->connect();
 $test->select_datebase("handsdb");
 $sql = "select * from book";
 $test->execute($sql);
 $result = $test->fetch_result();
 
 $count = count($result);
 echo $count;  // 8
 echo '<br />';
 $size = sizeof($result);
 echo $size;  // 8
 echo '<br />';
 for($i = 0;   $i < $count;  $i plus)
 bracket
 echo $result[$i];
 echo '<br />';
 bracket

The structure of my watch is like this

mysql> describe book;
 Plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus
 | Field    | Type                     | Null | Key | Default | Extra |
 Plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus
 | ID       | int(11)                  | NO   | PRI | 0       |       |
 | name     | varchar(7)               | NO   |     | NULL    |       |
 | price    | decimal(6,2)             | NO   |     | NULL    |       |
 | validity | enum('onsale','unvalid') | NO   |     | onsale  |       |
 Plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus-plus
 4 rows in set (0.01 sec)
mysql> select * from book;
 Plus-Plus-Plus-Plus-Plus-Plus-Plus-Plus
 | ID | name     | price | validity |
 Plus-Plus-Plus-Plus-Plus-Plus-Plus-Plus
 | 1 | Advanced Mathematics | 15.00 | onsale |
 | 2 | Linear Algebra | 10.50 | unvalid |
 Plus-Plus-Plus-Plus-Plus-Plus-Plus-Plus
 2 rows in set (0.00 sec)

Obviously, this table has only 4 columns. However, when testing the output, using MySQLI _ FETCH _ array() to take a row of the result array, we found that the array actually has 8 elements, and the latter 4 are empty and cannot be output. After the table is adjusted to 3 columns, the array elements become 6, which shows that the problem can be repeated and regular.

Excuse me
1. Why does the number of elements in an array become twice as many as the number of columns? Is MySQLI _ FETCH _ array() designed like this?
2. How to safely traverse and output all records without knowing all the column names of the table?

Just look at thisPHP mysql_fetch_array () function

mysqli_fetch_arrayThere are two parameters. When the second parameter is not specified, the default is to generate an association and a number array, which will output an array similar to the following structure

Array
 (
 [0] => Adams
 [LastName] => Adams
 [1] => John
 [FirstName] => John
 [2] => London
 [City] => London
 )

In addition, from various angles, database operations are recommended.PDO