Php foreach循环未进入
我的目标是让国家名称按字母顺序打印出来。这是我为它编写的函数Php foreach循环未进入,php,sql,loops,debugging,foreach,Php,Sql,Loops,Debugging,Foreach,我的目标是让国家名称按字母顺序打印出来。这是我为它编写的函数 function getCountries(){ $namesQ = 'SELECT Name FROM `country` ORDER BY Name ASC'; global $myPdo; $command = $myPdo -> prepare('namesQ'); $command -> execute(); return $command; } 然后,我
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>
。。。不打印到屏幕
我将
$countryName
中的索引更改为fhsdjk
,因为没有这样的索引,但我甚至没有收到错误消息或任何东西。我如何才能将它发送到echo
输出foreach
循环中的任何内容?您需要传递变量的传递字符串
$command = $myPdo -> prepare('namesQ');
(To)
$command = $myPdo->prepare($namesQ);
看起来您正在准备字符串
'namesQ'
,但实际上您希望准备分配给$namesQ
的sql语句。那么,替换
$command = $myPdo->prepare('namesQ');
与
我建议您将fetchAll()
调用包含到getCountries()
函数中,并仅调用:
$countryNames = getCountries();
而且,由于您在发现数据库访问错误时遇到一些问题,我建议您始终实现异常处理。尤其是当您使用PDO
作为数据访问抽象时。下面是一个示例-与您的代码类似:
function getCountries() {
try {
$namesQ = 'SELECT Name FROM `country` ORDER BY Name ASC';
global $myPdo;
// Hier: replaced 'namesQ' with $namesQ.
$command = $myPdo->prepare($namesQ);
if (!$command) {
throw new Exception('The SQL statement can not be prepared!');
}
if (!$command->execute()) {
throw new Exception('The PDO statement can not be executed!');
}
return $command->fetchAll();
} catch (PDOException $pdoException) {
echo '<pre>' . print_r($pdoException, true) . '</pre>';
exit();
} catch (Exception $exception) {
echo '<pre>' . print_r($exception, true) . '</pre>';
exit();
}
}
函数getCountries(){
试一试{
$namesQ='从'country`按名称排序ASC'中选择名称;
全球$多年电价确定;
//Hier:将“namesQ”替换为$namesQ。
$command=$myPdo->prepare($namesQ);
if(!$命令){
抛出新异常('SQL语句无法准备!');
}
如果(!$command->execute()){
抛出新异常('PDO语句无法执行!');
}
返回$command->fetchAll();
}捕获(PDO异常$PDO异常){
回显“”。打印错误($PDO异常,true)。“”;
退出();
}捕获(异常$Exception){
回显“”。打印($exception,true)。“”;
退出();
}
}
也许我的一个关于异常处理的早期回答也会有所帮助:
您的passig字符串需要传递varibale$command=$myPdo->prepare($namesQ);
$countryNames = getCountries();
function getCountries() {
try {
$namesQ = 'SELECT Name FROM `country` ORDER BY Name ASC';
global $myPdo;
// Hier: replaced 'namesQ' with $namesQ.
$command = $myPdo->prepare($namesQ);
if (!$command) {
throw new Exception('The SQL statement can not be prepared!');
}
if (!$command->execute()) {
throw new Exception('The PDO statement can not be executed!');
}
return $command->fetchAll();
} catch (PDOException $pdoException) {
echo '<pre>' . print_r($pdoException, true) . '</pre>';
exit();
} catch (Exception $exception) {
echo '<pre>' . print_r($exception, true) . '</pre>';
exit();
}
}