在使用MySQL一段时间后,我才开始涉足MySQLi。我在教程中找到了这个小代码段,并且对此有一个疑问(因为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'];
还是现在有完全不同的东西?
例如; 我们有“名称”,“人口”,“大陆”列可供选择。
Name | Population | Continent
X | 1 | A
Y | 2 | B
Z | 3 | C
这些是此查询的结果返回的行。
在第一次迭代中,您将得到$row
这样的
array("Name"=> "X", "Population"=>"1", "Continent"=>"A");
在第二次迭代中,您将得到$row
这样的结果
array("Name"=> "Y", "Population"=>"2", "Continent"=>"B");
等等。因此,您可以按照您提到的方式使用它(例如$row["Name"]
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句