mysqli_fetch_row()、assoc()和array()的区别

使用mysqli_fetch_row()、mysqli_fetch_assoc()和mysqli_fetch_array() 都是把查询结果返回到一个数组中。

概念

  1. mysqli_fetch_row() 函数从结果集中取得一行作为索引数组。
  2. mysqli_fetch_assoc() 函数从结果集中取得一行作为关联数组。
  3. mysqli_fetch_array() 函数用来使用或输出所有查询的数据。从结果集中取得一行作为关联数组,或索引数组,或二者兼有。

实例1

$con_db = mysqlInit('127.0.0.1','root','root','xk_mall');
if (!$con_db){
    echo mysqli_errno($con_db);
    exit;
}

//根据用户名查询用户
$sql = "SELECT * FROM xk_user WHERE username='{$username}' LIMIT 1";
$obj = mysqli_query($con_db,$sql);
$result = mysqli_fetch_row($obj);
print_r($result);

结果

Array ( 
    [0] => 2 
    [1] => root 
    [2] => 091301e077e3903eac8dd87d76adae91 
    [3] => 1535788912 
)

实例2

$con_db = mysqlInit('127.0.0.1','root','root','xk_mall');
if (!$con_db){
    echo mysqli_errno($con_db);
    exit;
}

//根据用户名查询用户
$sql = "SELECT * FROM xk_user WHERE username='{$username}' LIMIT 1";
$obj = mysqli_query($con_db,$sql);
$result = mysqli_fetch_assoc($obj);
print_r($result);

结果

Array ( 
    [id] => 2 
    [username] => root 
    [password] => 091301e077e3903eac8dd87d76adae91 
    [create_time] => 1535788912 
)

实例3

$con_db = mysqlInit('127.0.0.1','root','root','xk_mall');
if (!$con_db){
    echo mysqli_errno($con_db);
    exit;
}

//根据用户名查询用户
$sql = "SELECT * FROM xk_user WHERE username='{$username}' LIMIT 1";
$obj = mysqli_query($con_db,$sql);
$result = mysqli_fetch_array($obj);
print_r($result);

结果

Array (
    [0] => 2 
    [id] => 2 
    [1] => root 
    [username] => root 
    [2] => 091301e077e3903eac8dd87d76adae91 
    [password] => 091301e077e3903eac8dd87d76adae91 
    [3] => 1535788912 
    [create_time] => 1535788912 
)