mysql_fetch_assoc这个扩展在 PHP 5.5.0 中被弃用,在 PHP 7.0.0 中被移除。而是使用mysqli_fetch_assoc - 这将获取结果行作为关联数组。
示例 1:面向对象的风格
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 1";
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
echo $row['Name'];
示例 2:程序风格
$link = mysqli_connect("localhost", "my_user", "my_password", "dbname");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 1";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
$row = mysqli_fetch_assoc($result);
echo $row['Name'];
}