我们都知道,
如果函数成功执行,它将获取一行信息(该行是通过执行mysql_query()函数取得的)并返回该行信息;如果失败,将不返回任何一行。
PHP mysql_fetch_array()函数语法
mysql_fetch_array(data,array_type)
data为必要参数,指定需要使用的数据指针[data pointer]。该数据指针是通过请求mysql_query()函数返回的。
array_type为可选参数。指定返回的数组类型。可选参数如下:
* MYSQL_ASSOC – 关联型数组[Associative array]
*MYSQL_NUM – 数值型数组[Numeric array]
* MYSQL_BOTH – 默认值。包含关联型数组[Associative array]和数值型数组[Numeric array]
PHP mysql_fetch_array()函数提示和注意点
注意:当指定的数据获取之后,该函数将指向下一条记录;如果接下来继续请求mysql_fetch_array()函数,那么将返回下一条记录。
提示:通过mysql_fetch_array()函数返回的字段名将不区分字母大小写。
PHP mysql_fetch_array()函数案例
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li> <span>$</span><span class="attribute">con</span><span> = </span><span class="attribute-value">mysql_connect</span><span>("localhost", <br>"peter", "abc123"); </span> </li> <li class="alt"><span>if (!$con) </span></li> <li><span>{ </span></li> <li class="alt"><span>die('Could not connect: ' . <br>mysql_error()); </span></li> <li><span>} </span></li> <li class="alt"> <span>$</span><span class="attribute">db_selected</span><span> = </span><span class="attribute-value">mysql_select_db<br></span><span>("test_db",$con); </span> </li> <li> <span>$</span><span class="attribute">sql</span><span> = </span><span class="attribute-value">"SELECT * from Person <br>WHERE Lastname='Refsnes'"</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">mysql_query</span><span>($sql,$con); </span> </li> <li><span>print_r(mysql_fetch_array($result)); </span></li> <li class="alt"><span>mysql_close($con); </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
上述代码将输出下面的结果:
<ol class="dp-xml"> <li class="alt"><span><span>Array </span></span></li> <li><span>( </span></li> <li class="alt"> <span>[0] =</span><span class="tag">></span><span> Refsnes </span> </li> <li> <span>[LastName] =</span><span class="tag">></span><span> Refsnes </span> </li> <li class="alt"> <span>[1] =</span><span class="tag">></span><span> Kai Jim </span> </li> <li> <span>[FirstName] =</span><span class="tag">></span><span> Kai Jim </span> </li> <li class="alt"> <span>[2] =</span><span class="tag">></span><span> Taugata 2 </span> </li> <li> <span>[Address] =</span><span class="tag">></span><span> Taugata 2 </span> </li> <li class="alt"> <span>[3] =</span><span class="tag">></span><span> 22 </span> </li> <li> <span>[Age] =</span><span class="tag">></span><span> 22 </span> </li> <li class="alt"><span>) </span></li> </ol>
以上就是PHP mysql_fetch_array()函数的具体使用方法。