我正在尝试在满足条件时从表中获取行(状态='没有转接')但是即使应该显示行(计数为1和更多)也不会显示任何内容。
if($query['num'] == 0){
echo "<p>No shopping orders on transit</p>";
}else{
$sql = "SELECT *, FORMAT(total, 0) AS total, FORMAT(grand_total, 0) AS grand_total FROM shipping_details WHERE status = 'no transit' ORDER BY order_id DESC";
foreach ($db->query($sql) AS $query){
echo" Show some results ";
$select = "SELECT * FROM shipping_order WHERE order_id = :order_id";
foreach ($db->query($select, array('order_id' => $query['order_id'])) AS $items){
echo"
Some results
";
//Foreach ends
}
}
}
答案 0 :(得分:0)
它无效,因为您忘记使用pdoStatemnt类中的prepare和execute方法。
见下文:
$stmt = $db->prepare("SELECT * FROM shipping_order WHERE order_id = :order_id");
$stmt->execute(array('order_id' => $query['order_id']));
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)){
echo"
Some results
";
//Foreach ends
}
答案 1 :(得分:0)
您没有足够的说明我们可以告诉您使用哪个代码库连接到您的数据库(MySQLi,mysql_或PDO),因此下面的代码可能需要进行一些调整。
问题基本上是你永远不会检索你的数据库结果。相反,您尝试循环查询执行本身。
更改
$sql = "SELECT *...";
foreach ($db->query($sql) AS $query)...
要
$sql = "SELECT *...";
$result = $db->query($sql); //execute the query
if(!$result) die($db->error); //exit and show error if query failed
//now we can fetch the results one at a time and loop through them
//this line may need to be adjusted if you're not using MySQLi
while($row = $result->fetch_assoc())...
在while
循环中,$row
将包含DB记录中的值。使用print_r($row)
了解其形状。