PHP / MYSQL - 数据检索

时间:2015-06-16 09:25:56

标签: php mysql

根据join比嵌套查询更好的建议,我已将所有嵌套查询转换为join。但是,转换为join后,我无法从SQL结果中检索数据到我的数组中。

以下是我的疑问:

不加入

$a="SELECT F_DATE, COUNT(F_DATE) as COUNT_F 
    from FWH 
    where FI_NAME IN 
    ( 
       SELECT I_NAME from INS_W WHERE INSTANCE_ID IN 
       (
         SELECT I_MAP_ID FROM T_MAP where T_MAP_ID = 
         (
           SELECT T_ID FROM TWY WHERE T_NAME = 'abc'
          )
        )
    ) 
    AND F_DATE between '$S_D' AND '$E_D'
    GROUP BY F_DATE";

加入

$a="SELECT t1.F_DATE AS DATE_F, COUNT(t1.F_DATE) as COUNT_F
    from FWH t1 
    JOIN INS_W t2 ON(t1.FI_NAME = t2.I_NAME) 
    JOIN T_MAP t3 ON(t2.INSTANCE_ID = t3.I_MAP_ID) 
    JOIN TWY t4 ON(t3.T_MAP_ID = t4.T_ID) 
    WHERE t4.T_NAME = 'abc' AND
    t1.F_DATE BETWEEN '$S_D' AND 'E_D'GROUP BY t1.F_DATE";

这里是检索数据的PHP代码

$link = mysql_connect("ip", "user", "passs");
$dbcheck = mysql_select_db("db");   
if ($dbcheck) {
    $chart_array_1[] = "['F DATE','F COUNT']";
    $result = mysql_query($a);
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
            $f_date=$row["DATE_F"];
            $f_count=$row["COUNT_F"];
            $chart_array_1[]="['".$f_date."',".$f_count."]";
        }
    }
}
mysqli_close($link);

直接在MySQL DB上测试时,SQL查询本身运行正常。

1 个答案:

答案 0 :(得分:0)

出于某种原因,当我使用连接时,我被迫使用row [0],row [1]等而不是使用列名来获取值。我不明白这背后的原因。但是,这是我的唯一出路。以下代码适用于那些可能遇到与我类似情况的人。

$link = mysql_connect("ip", "user", "passs");
$dbcheck = mysql_select_db("db");   
if ($dbcheck) {
    $chart_array_1[] = "['F DATE','F COUNT']";
    $result = mysql_query($a);
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
            $chart_array_1[]="['".$row[0]."',".$row[1]."]";
        }
    }
}
mysqli_close($link);