Home > Backend Development > PHP Tutorial > 关于mysql_fetch_array 的问题

关于mysql_fetch_array 的问题

WBOY
Release: 2016-06-23 14:09:12
Original
1091 people have browsed it

我从数据库中获取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);
Copy after login

代码就像上面那样,当我不用while时,打印结果只有$row[0]有值,从$row[1]开始之后都没有值。用while时就显示所有值都存在$row[0]里面,其他元素都没有值。


回复讨论(解决方案)

不用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连接到页面上的关于mysql_fetch_array 的问题去显示出来

楼上正解,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";}
Copy after login
Copy after login

while ($row=mysql_fetch_array($result)) {      echo "<img  src='$row[0]' / alt="关于mysql_fetch_array 的问题" >\n";}
Copy after login
Copy after login

我就是想把这6个url放到数组中6个元素里面好供我使用,怎么弄?

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);?>
Copy after login


一维数组????
这样应该就没问题了

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template