對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2014/3/5 9:12:02
Example #1
Example #2
以上例程会输出:
Example #3
以上例程的输出类似于:
冷日:簡單來說,就是可以直接用 Table 的 Colnum Name 來做 index 取得 Row 裡面該行的內容!
原文出處:PHP: mysqli_result::fetch_assoc - Manual
<?php
$mysql = new mysqli("localhost", "my_user", "my_password", "world");
/* 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 50,5";
if ($result = $mysql->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
$result->free();
}
/* close connection */
$mysql->close();
?>
Example #2
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* 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 50,5";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
?>
以上例程会输出:
Pueblo (USA)
Arvada (USA)
Cape Coral (USA)
Green Bay (USA)
Santa Clara (USA)
Example #3
<?php
$c = mysqli_connect('127.0.0.1','user', 'pass');
// Using iterators (support was added with PHP 5.4)
foreach ( $c->query('SELECT user,host FROM mysql.user') as $row ) {
printf("'%s'@'%s'\n", $row['user'], $row['host']);
}
echo "\n==================\n";
// Not using iterators
$result = $c->query('SELECT user,host FROM mysql.user');
while ($row = $result->fetch_assoc()) {
printf("'%s'@'%s'\n", $row['user'], $row['host']);
}
?>
以上例程的输出类似于:
'root'@'192.168.1.1'
'root'@'127.0.0.1'
'dude'@'localhost'
'lebowski'@'localhost'
==================
'root'@'192.168.1.1'
'root'@'127.0.0.1'
'dude'@'localhost'
'lebowski'@'localhost'
冷日:簡單來說,就是可以直接用 Table 的 Colnum Name 來做 index 取得 Row 裡面該行的內容!
原文出處:PHP: mysqli_result::fetch_assoc - Manual