如何使用PHP从mysql获取所有数据

时间:2016-05-02 09:44:06

标签: php mysql

正如你在我的while循环中看到的那样,我声明我变量$ TYPES ..

这是我的第一个查询

    $first = "SELECT DISTINCT DATE_FORMAT(z.DatePaid,'%M %d, %Y') AS Paid 
    FROM tblStudPayments z 
    INNER JOIN tblPersonalData p ON p.StudNo=z.StudNo
    WHERE z.StudNo=p.StudNo AND z.SY='".$SY."' AND z.Sem='".$Sem."' ORDER BY z.DatePaid;";

    $fs = safe_query($first);
    $numrows = mysql_num_rows($fs);
    if($numrows>0)
    {
    while($dataf = mysql_fetch_assoc($fs))
    {
        $types =$dataf['Paid']; 

    }
    }

我想将$ TYPES的值传递给我的第二个查询

这是我的第二个查询

     $sql="SELECT DISTINCT p.StudNo, p.LName, p.FName, p.MName, p.NName, c.Description, p.YearLevel, d.Status, 
         '".$types."' AS DateEnlisted, 

        FROM tblPersonalData p 
        INNER JOIN tblStudPayments sp ON sp.StudNo=p.StudNo AND sp.SY='".$SY."' AND sp.Sem='".$Sem."' 
        INNER JOIN tblStatusHistory d ON d.StudNo=sp.StudNo AND d.SY=sp.SY AND d.Sem=sp.Sem 
        INNER JOIN tblCourses c ON c.CourseCode=d.CourseCode AND c.HSOrCollege='".$dType."' 
        INNER JOIN tblUserAcct u ON u.UserName=p.StudNo

        "; 


        $sql.=" HAVING DateEnlisted = '".$a['DateEnrolled']."' ";
        $sql.=" ORDER BY p.StudNo ASC;"; 

在SELECT语句的底部你可以看到我的变量$ TYPES我从第一个查询中得到它。

问题是它没有获取所有数据..它只从mysql获取最后一个数据

先谢谢.. [图片由Kundu推荐更新] [1]

[图片由Kundu echo $ sql建议] [2]

i try one query but the loading of data takes 10-15MIN.

2 个答案:

答案 0 :(得分:0)

在你的while循环中,你将值放在变量& evry time这个变量被更新。所以在循环结束时,它将返回最后type值。要么把它放在一个数组中,所以它会存储所有的值。但是当你把它放在第二个查询中时,你需要将数组转换成字符串。同时确保当你在$type中放入值时它应该是独特。

像这样更新您的代码:

$types=array();
 while($dataf = mysql_fetch_assoc($fs))
 {
      $types[] = $dataf['Paid']; 

 }

$allTypes = implode(",`", array_unique($types));

现在在您的查询中:

$sql="SELECT DISTINCT p.StudNo, p.LName, p.FName, p.MName, p.NName, c.Description, p.YearLevel, d.Status, 
         `".$allTypes."` AS DateEnlisted, 

        FROM tblPersonalData p 
        INNER JOIN tblStudPayments sp ON sp.StudNo=p.StudNo AND sp.SY='".$SY."' AND sp.Sem='".$Sem."' 
        INNER JOIN tblStatusHistory d ON d.StudNo=sp.StudNo AND d.SY=sp.SY AND d.Sem=sp.Sem 
        INNER JOIN tblCourses c ON c.CourseCode=d.CourseCode AND c.HSOrCollege='".$dType."' 
        INNER JOIN tblUserAcct u ON u.UserName=p.StudNo

        "; 

答案 1 :(得分:0)

将第二个查询放在第一个查询

 while 
循环