嵌套while循环不能正常运行php

时间:2014-11-19 05:53:06

标签: php while-loop

我想从我的sql中获取一些数据。但是$ arra_detail []的结果不断重复......这是我的代码..请查看它,让我知道我正在制作的错误..任何帮助将不胜感激..

//Process of getting data from table
$sql = "SELECT * FROM projects JOIN project_details ON projects.project_id=project_details.project_id";

$results = $conn->query($sql);

            if($results->num_rows > 0){
               while($row = $results->fetch_assoc()){

                   $pid = $row["project_id"];
                   $detailQuery = "SELECT * FROM project_details WHERE project_id = $pid";
                   $details = $conn->query($detailQuery);
                   if($details->num_rows > 0){
                       while ($r = $details->fetch_assoc()){
                           $projectDetails[] = array(
                               "project_detail" => $r["project_detail"],
                               "project_payment" => $r["project_payment"],
                               "project_payment_date" => $r["project_payment_date"],
                               "project_payment_status" => $r["project_payment_status"]
                           ); 
                       }
                   }

                   $data[$row["project_name"]] = array(
                       "project_id" => $row["project_id"],
                       "project_start_date" => $row["project_start_date"],
                       "project_due_date" => $row["project_due_date"],
                       "project_currency" => $row["project_currency"],
                       "poject_details" => $projectDetails
                   );


               }
               echo "<pre>",  print_r($data) , "</pre>";
            } else{
                echo "No data found ";
            }

1 个答案:

答案 0 :(得分:0)

使用内部联接查询而不是使用2循环

的两个查询
  

SELECT * FROM projects,project_details WHERE   projects.project_id = project_details .project_id