mysqli_fetch_array() 反回的是一維陣列
1. 用print_r或var_dump檢查$row得到的是一維陣列(第一筆資料)
#!/usr/bin/php
<?php
$user='username';
$passwd='password';
$host='host';
$dbname='dbname';
$conn=mysqli_connect($host, $user, $passwd, $dbname);
$sql="select * from tablename";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
print_r($row);
?>
用print_r或var_dump檢查$row得到的是一維陣列(第一筆資料)
array
(
[0] => value of field0
[field0] => value of field0
[1] => value of field1
[field1] => value of field1
)
2.若要取出所有資料放入二維陣列
while($row = mysqli_fetch_array($result)) {
$data[] = $row;
}
3. 一般來說列印資料庫資料只需如下:
while($row = mysqli_fetch_array($result))
{
echo $row[0].",".$row[1]."\n";
}
4. 如果要取出所有資料庫資料放入二維陣列,再列印如下:
#!/usr/bin/php
<?php
$user='username';
$passwd='password';
$host='host';
$dbname='dbname';
$conn=mysqli_connect($host, $user, $passwd, $dbname);
$sql="select * from tablename";
$result=mysqli_query($conn,$sql);
$length = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result))
{
$data[] = $row;
}
for ($i=0;$i<$length;$i++)
{
for ($j=0;$j<2;$j++) {
echo $data[$i][$j].",";
}
echo "\n";
}
?>