mysqli导致数组

时间:2014-02-11 21:05:46

标签: php mysql mysqli

我从查询SELECT * FROM myTable

生成此表
+--------------------------+---------+--------+
| Name                     | Version | Detail |
+--------------------------+---------+--------+
| name0                    |      10 | xxx    |
| name1                    |      30 | yyy    |
| name2                    |      30 | zzz    |
| name3                    |      30 | kkk    |
+--------------------------+---------+--------+

所以,我需要使用mysqli函数将这个表放在php数组中。

//$this->internalDB have the db connection
$result = $this->internalDB->query($query)->fetch_array(MYSQLI_NUM);
print_r($result);

产生

Array ( [Name] => name0 [Version] => 10 [Detail] => xxx )

循环,语音不会改变......

我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

这将查询数据库并将您的值作为$ arrReturn变量中的关联数组返回。

$mysqli = new mysqli("host", "user", "password", "database");

$query = "SELECT * FROM myTable";
$sqlRes = $mysqli->query($query);

if (is_object($sqlRes)) {
    $arrReturn = array();

    while ($row = $sqlRes->fetch_assoc()) {
        $arrRow = array();
        foreach($row as $field => $value) {
            $arrRow[$field] = $value;
        }
        $arrReturn[] = $arrRow;
    }

    echo $arrReturn[0]['Name']; // prints 'name0'
}

阅读mysqli quickstart guide

可能会有所帮助