php 操作mysql 获取select 结果的几种方式

如果用了 MYSQL_BOTH,将得到一个同时包含关联和数字索引的数组。

用 MYSQL_ASSOC 只得到关联索引(如同mysql_fetch_assoc() 那样),

用 MYSQL_NUM 只得到数字索引(如同 mysql_fetch_row 那样)。

1. mysql_fetch_array($rs,MYSQL_ASSOC)

[@test01 model]# php test.php 
Array
(
    [name] => hellokitty
    [addr] => i dont kno
)
[@test01 model]# more test.php 
<?php
    $link=mysql_connect("10.12.136.181","hello","hello");
    if(!$link) 
        echo "没有连接成功!";
    mysql_select_db("hhhhh", $link);
    $q = "SELECT * FROM hello";
    mysql_query("SET NAMES GB2312");
    $rs = mysql_query($q);
    if(!$rs)
    {
        die("Valid result!");
    }
    $result=mysql_fetch_array($rs,MYSQL_ASSOC);
    print_r($result);
    mysql_free_result($rs);
?>


2.mysql_fetch_array($rs,MYSQL_BOTH);获取数组

[@test01 model]# more test.php 
<?php
    $link=mysql_connect("10.12.136.181","hello","hello");
    if(!$link) 
        echo "没有连接成功!";
    mysql_select_db("hhhhh", $link);
    $q = "SELECT * FROM hello";
    mysql_query("SET NAMES GB2312");
    $rs = mysql_query($q);
    if(!$rs)
    {
        die("Valid result!");
    }
    $result=mysql_fetch_array($rs,MYSQL_ASSOC);
    print_r($result);
    mysql_free_result($rs);
?>
[@test01 model]# vim test.php 
[@test01 model]# php test.php 
Array
(
    [0] => hellokitty
    [name] => hellokitty
    [1] => i dont kno
    [addr] => i dont kno
)
[@test01 model]# 

3.mysql_fetch_array($rs,MYSQL_NUM) 获取数组
[@test01 model]# php test.php 
Array
(
    [0] => hellokitty
    [1] => i dont kno
)
[@test01 model]# more test.php 
<?php
    $link=mysql_connect("10.12.136.181","hello","hello");
    if(!$link) 
        echo "没有连接成功!";
    mysql_select_db("hhhhh", $link);
    $q = "SELECT * FROM hello";
    mysql_query("SET NAMES GB2312");
    $rs = mysql_query($q);
    if(!$rs)
    {
        die("Valid result!");
    }
    $result=mysql_fetch_array($rs,MYSQL_NUM);
    print_r($result);
    mysql_free_result($rs);
?>
[@test01 model]# 



你可能感兴趣的:(mysql)