如何将这个php foreach echo变成变量?
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT town FROM shows';
foreach ($pdo->query($sql) as $row) {
echo "' " . $row['town'] . " ' ,";
}
Database::disconnect();
?>
非常感谢
答案 0 :(得分:2)
简单连接。 (假设您的查询确实有效并且有些条目被查询,并且您的问题是“回显”到变量中)
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT town FROM shows';
$foo = "";
foreach ($pdo->query($sql) as $row) {
$foo .= "' " . $row['town'] . " ' ,";
}
echo $foo;
Database::disconnect();
?>
答案 1 :(得分:1)
如果你想让它成为一个数组,你可以这样做:
$myarray[]="' " . $row['town'] . " ' ,";
之后,您可以使用echo $myarray[(numberofrow)];
如果您希望它是一个变量:
$myvar .= "' " . $row['town'] . " ' ,";