【发布时间】:2014-01-11 23:43:30
【问题描述】:
在使用 MySQL 一段时间后,我才刚刚开始接触 MySQLi。我在一个教程中找到了这个小代码 sn-p,并且对此有疑问(因为 Google 不会为我返回任何结果......)。
<?php
$link = mysqli_connect("127.0.0.1", "user", "password", "world");
if (!$link)
{
$error = mysqli_connect_error();
$errno = mysqli_connect_errno();
print "$errno: $error\n";
exit();
}
$query = "SELECT Name, Population, Continent FROM Country WHERE Continent=? ORDER BY Name LIMIT 1";
$stmt = mysqli_stmt_init($link);
if(!mysqli_stmt_prepare($stmt, $query))
{
print "Failed to prepare statement\n";
}
else
{
mysqli_stmt_bind_param($stmt, "s", $continent);
$continent_array = array('Europe','Africa','Asia','North America');
foreach($continent_array as $continent)
{
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_array($result, MYSQLI_NUM))
{
foreach ($row as $r)
{
print "$r ";
}
print "\n";
}
}
}
mysqli_stmt_close($stmt);
mysqli_close($link);
?>
感谢http://www.php.net/manual/en/mysqli-stmt.get-result.php
如果我正确理解了这一点,那么$r 现在将创建一个数组,其中包含在所有调用行中找到的所有结果。如何为每一行分配一个变量?我还能像在 MySQL 中那样做类似的事情吗?
$username = $row['username'];
或者现在有什么完全不同的东西?
【问题讨论】: