【发布时间】:2017-10-22 20:44:20
【问题描述】:
我的目标是让国家名称按字母顺序打印出来。这是我为此编写的函数...
function getCountries(){
$namesQ = 'SELECT Name FROM `country` ORDER BY Name ASC';
global $myPdo;
$command = $myPdo -> prepare('namesQ');
$command -> execute();
return $command;
}
然后,我从 HTML 的数组中回显名称...
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Country Names</title>
<link type="text/css" rel="stylesheet" href="primary.css" />
</head>
<body>
<div id="main" action="controller.php" method="post">
<?php
$countries = getCountries();
$countryNames = $countries->fetchAll();
foreach( $countryNames as $countryName )
{
echo 'test';
echo '<p> ' . $countryName['Name'] . '</p>';
}
?>
</div>
</body>
</html>
但似乎根本无法访问foreach 循环,因为即使...
echo 'test';
... 不打印到屏幕上。
我将$countryName 中的索引更改为fhsdjk,因为没有这样的索引,但我什至没有收到错误消息或任何东西。如何将echo 从foreach 循环内的任何内容中取出?
【问题讨论】:
-
你需要传递变量的密码字符串 $command = $myPdo -> prepare($namesQ);
标签: php sql loops debugging foreach