php mysqli_fetch_assoc()函数用法实例介绍

时间:2017-02-22
php mysqli_fetch_assoc()函数获取结果行作为关联数组,本文章向大家介绍php mysqli_fetch_array()函数的基本语法和使用实例,需要的朋友可以参考一下。

定义

mysqli_fetch_assoc()函数获取结果行作为关联数组。

语法

mysqli_fetch_assoc(result);

参数

参数 是否必须 描述
result 需要。 由mysqli_query(),mysqli_store_result()或mysqli_use_result()返回的结果集

返回值

它返回一个表示所取行的字符串的关联数组。如果结果集中没有更多行,则为NULL。

实例

以下代码将结果行作为关联数组提取。

<?php
//  http://www.manongjc.com/article/1658.html
//  作者:码农教程
$con=mysqli_connect("localhost","my_user","my_password","my_db");

if (mysqli_connect_errno($con)){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT name,salary FROM emp";
$result=mysqli_query($con,$sql);

$row=mysqli_fetch_assoc($result);
print $row["name"];
print "\n";
print $row["salary"];

mysqli_free_result($result);

mysqli_close($con);
?>