php - 显示 MySQL 表中的行时出错

标签 php mysql

当满足条件(状态=“无传输”)时,我试图从表中获取行,但即使应该显示行(计数为 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
              }
            }
         }

最佳答案

您没有充分展示我们可以告诉您使用哪个代码库连接到数据库(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 将包含数据库记录中的值。使用 print_r($row) 了解其形状。

关于php - 显示 MySQL 表中的行时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38558835/

相关文章:

javascript - 如何在 “onclick” 事件期间加载外部 php 文件?

php - 为什么 TRANSACTION/COMMIT 使用 PHP/MySQL (InnoDB) 提高了性能?

php - 修复 undefined index

php - 使用 PHP 时出现错误 : "Duplicate entry ' 0' for key ' PRIMARY'"in MySql,

mysql - docker commit mysql 不保存

php - 当数据库中的特定数据发生更改时使用 php 显示通知

php - 从 mySQL 数据库显示单个值的更有效方法?

mysql - 确保完全不相交特化 MySQL/JPA

mysql - 组合 LEFT/INNER JOIN + GROUP BY 时缺少 SQL 条目

mysql - 从一个表中选择列值存在于另一个表中的记录