What is the size of the array returned by mysqli_fetch_array?

Source: Internet
Author: User
For ease of use, I have encapsulated the mysqli method: {code ...} the structure of my table is as follows: {code ...} {code ...} obviously, this table has only four columns. However, when testing the output, we use mysqli_fetch_array () to retrieve a row of result arrays. We found that the array actually has... for ease of use, I have encapsulated the mysqli method:


  host = "localhost";    $this->username = "hands";    $this->password = "hands";    }        function __destruct()    {    mysqli_close($this->link);    }        function connect()    {    $this->link = mysqli_connect($this->host, $this->username, $this->password);    if(!$this->link)    {        echo mysqli_connect_error();    }    }        function select_datebase($database)    {    mysqli_select_db($this->link, $database);    }            function execute($sql)    {    $this->resultSet = mysqli_query($this->link, $sql);    return $this->resultSet;    }        function getResultSet()    {    return $this->resultSet;    }        function fetch_result()    {    $row = mysqli_fetch_array($this->resultSet);    return $row;    }    }$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;                // 8echo '
';$size = sizeof($result);echo $size; // 8echo '
';for($i = 0; $i < $count; $i++){ echo $result[$i]; echo '
';}

The structure of my table is as follows:

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

Obviously, this table has only four columns. However, when testing the output, we use mysqli_fetch_array () to retrieve a row of result arrays. We found that the array actually has eight elements, and the last four are empty, unable to output. After the table is changed to three columns, the array elements are changed to six, which indicates that the problem can be reproduced and the problem is regular.

Excuse me
1. Why does the number of elements in the array change to twice the number of columns? Is mysqli_fetch_array () designed in this way?
2. How can I safely traverse and output all records without knowing all column names in the table?

Reply content:

For ease of use, I have encapsulated the mysqli method:


  host = "localhost";    $this->username = "hands";    $this->password = "hands";    }        function __destruct()    {    mysqli_close($this->link);    }        function connect()    {    $this->link = mysqli_connect($this->host, $this->username, $this->password);    if(!$this->link)    {        echo mysqli_connect_error();    }    }        function select_datebase($database)    {    mysqli_select_db($this->link, $database);    }            function execute($sql)    {    $this->resultSet = mysqli_query($this->link, $sql);    return $this->resultSet;    }        function getResultSet()    {    return $this->resultSet;    }        function fetch_result()    {    $row = mysqli_fetch_array($this->resultSet);    return $row;    }    }$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;                // 8echo '
';$size = sizeof($result);echo $size; // 8echo '
';for($i = 0; $i < $count; $i++){ echo $result[$i]; echo '
';}

The structure of my table is as follows:

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

Obviously, this table has only four columns. However, when testing the output, we use mysqli_fetch_array () to retrieve a row of result arrays. We found that the array actually has eight elements, and the last four are empty, unable to output. After the table is changed to three columns, the array elements are changed to six, which indicates that the problem can be reproduced and the problem is regular.

Excuse me
1. Why does the number of elements in the array change to twice the number of columns? Is mysqli_fetch_array () designed in this way?
2. How can I safely traverse and output all records without knowing all column names in the table?

You can see the PHP mysql_fetch_array () function.

mysqli_fetch_arrayThere are two parameters. If the second parameter is not specified, the join and number arrays are generated by default. An array similar to the following structure is output.

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

Insert one more sentence. from a variety of perspectives, database operations are recommended.PDO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.