Maison >développement back-end >tutoriel php >关于mysql_fetch_array 的问题
我从数据库中获取6个图片url,然后用mysql_fetch_array将获取的值传给数组$row,但是打印$row时显示所取得6个url全都集中在$row[0]中,打印$row[1]时就显示错误。代码如下:
$conn=mysql_connect("localhost","root");mysql_select_db("xinshi");$sql="select image from photo order by id desc limit 6";$result=mysql_query($sql);//while ($row=mysql_fetch_array($result)){//print_r($row);}$row=mysql_fetch_array($result,MYSQL_NUM);print_r($row); mysql_close($conn);
不用while时,mysql_fetch_array 只是返回一条记录的数组,所以只有0有值
当用while时,while ($row=mysql_fetch_array($result)) 始终都在改变$row的值,$row本身的数组元素个数并没有改变。
楼上正解,LZ要数组的话可以这么写
include "mysqlconnect.php";
$sql ='select mealuid from mealtable';
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result)) {
$arr[]=$row;
}
print_r($arr);
?>
不用while时,mysql_fetch_array 只是返回一条记录的数组,所以只有0有值
当用while时,while ($row=mysql_fetch_array($result)) 始终都在改变$row的值,$row本身的数组元素个数并没有改变。
那我如何取出这6个值?我要把这6个url连接到页面上的去显示出来
楼上正解,LZ要数组的话可以这么写
include "mysqlconnect.php";
$sql ='select mealuid from mealtable';
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result)) {
$arr[]=$row;
}
print_r($arr);
?>
打印出来是这样的
Array (
[0] => Array ( [0] => upload/2013/05/08/14303.jpeg [image] => upload/2013/05/08/14303.jpeg )
[1] => Array ( [0] => upload/2013/05/03/14297 [image] => upload/2013/05/03/14297 )
[2] => Array ( [0] => upload/2013/05/03/14299.i [image] => upload/2013/05/03/14299.i )
[3] => Array ( [0] => upload/2013/05/03/14301.a [image] => upload/2013/05/03/14301.a )
[4] => Array ( [0] => upload/2013/05/03/137.image/jpeg [image] => upload/2013/05/03/137.image/jpeg )
[5] => Array ( [0] => upload/2013/05/03/14302image/jpeg [image] => upload/2013/05/03/14302image/jpeg )
)
这是怎么回事?这个结构什么意思?
while ($row=mysql_fetch_array($result)) { echo "<img src='$row[0]' / alt="关于mysql_fetch_array 的问题" >\n";}
while ($row=mysql_fetch_array($result)) { echo "<img src='$row[0]' / alt="关于mysql_fetch_array 的问题" >\n";}
while ($row=mysql_fetch_array($result,MYSQL_NUM)) {
$arr[]=$row;
}
print_r($arr);
<?phpinclude "mysqlconnect.php";$sql ='select mealuid from mealtable';$result=mysql_query($sql);while ($row=mysql_fetch_array($result)) { $arr[] = $row[0];}print_r($arr);?>