<分区>
我正在尝试创建一个将放入 foreach 语句中的字符串。该字符串将确定哪些字段将出现在数据库中。由于我无法提前确定将从表单中选择哪些字段,因此我认为这将是处理未知数并显示查询结果的最佳方式。
我以同样的方式构建了查询,并且成功了。我试过双引号和单引号,但我只是不明白为什么它不想构建这个字符串。请帮助(如果这是一个愚蠢的问题,请原谅,因为我是 PHP 的新手 - 我可以找到与类似搜索匹配的答案)。谢谢你! :-)
<?php
include('database.php');
function show_products($table, $productIDcb, $categoryIDcb, $productCodecb, $productNamecb, $listPricecb)
{
$list = "";
global $db;
$theQuery = 'select ';
if($productIDcb == "")
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'productID, ';
$list .=' $products['productID']'; //THIS IS LINE 17
}
if($categoryIDcb == "")
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'categoryID, ';
$list .=' $products['categoryID']';
}
if($productCodecb == "")
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'productCode, ';
$list .=' $products['productCode']';
}
if(isset($_POST['productNamecb']))
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'productName, ';
$list .=' $products['productName']';
}
if(isset($_POST['listPricecb']))
{
$theQuery .= 'listPrice, ';
}
else
{
$theQuery .= 'listPrice, ';
$list .=' $products['listPrice']';
}
$theQuery .=' "" from ' .$table;
echo($theQuery);
$rSet = $db -> query($theQuery);
foreach($rSet AS $products)
{
$list .= "<br>";
}
echo($list);
}
?>