mysql_fetch_array详解

http://wweihai.blog.sohu.com/76216338.html

(php3,php4)

mysql_fetch_array -- 从结果集中取得一行作为关联数组,或数字数组,或二者兼有.

array mysql_fetch_array(resource result[,int result_type])

mysql_fetch_array()中可选取的第二个参数result_type是一个常量,可以接受以下值:MYSQL_ASSOC,MYSQL_NUM和MYSQL_BOTH. 默认值是MYSQL_BOTH

如果用MYSQL_BOTH,将得到一个同时包含关联和数字索引的数组.用MYSQL_ASSOC只得到关联索引(如同mysql_fetch_assoc()那样),用MYSQL_NUM只得到数字索引(如同mysql_fetch_row()那样).

例1>mysql_fetch_array使用MYSQL_NUM

<?php

mysql_connect("localhost","mysql_user","mysql_password") or die("Could not connect:".mysql_error());

mysql_select_db("mydb");

$result=mysql_query("select id,name from mytable");

while($row=mysql_fetch_array($result,MYSQL_NUM)){

    printf("ID:%s Name:%s ",$row[0],$row[1]);

}

mysql_free_result($result);

?>

 

例2>mysql_fetch_array使用MYSQL_ASSOC

<?php

mysql_connect("localhost","mysql_user","mysql_password") or die("Could not connect:".mysql_error());

mysql_select_db("mydb");

$result=mysql_query("select id,name from mytable");

while($row=mysql_fetch_array($result,MYSQL_ASSOC)){

    printf("ID:%s Name:%s ",$row["id"],$row["name"]);

}

mysql_free_result($result);

?>

 

例3>mysql_fetch_array使用MYSQL_BOTH

<?php

mysql_connect("localhost","mysql_user","mysql_password") or die("Could not connect:".mysql_error());

mysql_select_db("mydb");

$result=mysql_query("select id,name from mytable");

while($row=mysql_fetch_array($result,MYSQL_BOTH)){

    printf("ID:%s Name:%s ",$row[0],$row["name"]);

}

mysql_free_result($result);

?>

你可能感兴趣的:(mysql)