2016-07-04 38 views
0

這裏是sql查詢,它在phpmyadmin上測試時工作正常。 下面是截圖。查詢返回空的結果在PHP中,但工作正常否則

SQL query working on phpmyadmin

下面是PHP代碼和它

<?php 

    header('Content-type=application/json;charset=utf-8'); 

    include("connection.php"); 
    session_start(); 
    if($_SERVER["REQUEST_METHOD"] == "POST") 
{ 

    $event_date1= mysqli_real_escape_string($con,$_POST['event_date1']); 
    $event_date2= mysqli_real_escape_string($con,$_POST['event_date2']); 



    $query = ("SELECT topic1 as topic from feedbackform_db where topic1 is not null AND event_date between $event_date1 AND $event_date2 
     union SELECT topic2 as topic from feedbackform_db where topic2 is not null AND event_date between $event_date1 AND $event_date2 
     union SELECT topic3 as topic from feedbackform_db where topic3 is not null AND event_date between $event_date1 AND $event_date2 
     union SELECT topic4 as topic from feedbackform_db where topic4 is not null AND event_date between $event_date1 AND $event_date2 
     union SELECT topic5 as topic from feedbackform_db where topic5 is not null AND event_date between $event_date1 AND $event_date2 
     union SELECT topic6 as topic from feedbackform_db where topic6 is not null AND event_date between $event_date1 AND $event_date2 
     union SELECT topic7 as topic from feedbackform_db where topic7 is not null AND event_date between $event_date1 AND $event_date2 
     union SELECT topic8 as topic from feedbackform_db where topic8 is not null AND event_date between $event_date1 AND $event_date2 "); 



    $response=mysqli_query($con,$query); 

    if($response) 
    echo "true"; 
    else 
    echo "false"; 


    // set array 
    $array = array(); 

    // look through query 
    while($row = mysqli_fetch_assoc($response)) 
    { 

    // add each row returned into an array 
    $array[] = $row; 

    // OR just echo the data: 
    echo $row['topic']; // etc 
    } 

    $data['details']=$array; 
    echo json_encode($data); 

    print_r($array); 
    // echo json_encode($array); 
} 

mysqli_close($con); 

?> 



<html> 
<head> 
    <title>client welcome Page</title> 

    <style type = "text/css"> 

     body \{ 

      font-family:Arial, Helvetica, sans-serif; 

      font-size:14px; 

     \} 

     label \{ 

      font-weight:bold; 

      width:100px; 

      font-size:14px; 

     \} 



     .box \{ 

      border:#666666 solid 1px; 

     \} 

     </style> 



    </head> 



    <body bgcolor = "#FFFFFF"> 



     <div align = "center"> 

     <div style = "width:300px; border: solid 1px #333333; " align = "left"> 

      <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div> 



      <div style = "margin:30px"> 



       <form action = "" method = "post"> 


        <label>event_date1:</label><input type = "text" name = "event_date1" class = "box" /><br/><br /> 
        <label>event_date2:</label><input type = "text" name = "event_date2" class = "box" /><br/><br /> 






        <input type = "submit" value = " Submit "/><br /> 

       </form> 



       <div style = "font-size:11px; color:#cc0000; margin-top:10px"></div> 



      </div> 



     </div> 



     </div> 


    </body> 

</html> 

當我在localhost.Result運行此HTML表單是空的,沒有錯誤。 但查詢運行它的表現「真」爲響應變量

這是結果

真{「細節」:[]}陣列()

爲什麼我在沒有得到值數組?

+1

檢查什麼是呼叫的MySQL前的'$ query'值。 – Alex

+0

空結果集不是錯誤。這是恰好不包含行的完美有效結果。你需要檢查mysqli_num_rows()來查看你匹配了多少行,這無疑會回到'0',留下一個空數組。 –

+0

確保您獲得活動日期值? –

回答

1

你的PHP代碼都不可能重複的,你必須在phpMyAdmin的事情:escape()呼叫確實報價值給你,所以你產生

SELECT ... event_date between 2016-06-03 and 2016-06-08 

注意到周圍缺乏報價相當於你日期。這意味着他們日期儘可能的MySQL而言,他們的數學加減法運算,和你正在做的相當於:

SELECT ... event_date between 2007 and 2002 

由於這是一個不可能的條件滿足(between必須smaller AND larger,你有larger AND smaller),你得到0行,你仍然得到一個有效的結果句柄,並最終得到一個空的數組。

你應該有

$query = ("SELECT [...snip..] between '$event_date1' AND '$event_date2' 

(注意'引號)爲您的會員選擇的查詢中的每一個。

+0

It Worked.thanks –

1

嘗試增加單引號'$event_date1' AND '$event_date2'

$query = ("SELECT topic1 as topic from feedbackform_db where topic1 is not null AND event_date between '$event_date1' AND '$event_date2' 
     union SELECT topic2 as topic from feedbackform_db where topic2 is not null AND event_date between '$event_date1' AND '$event_date2' 
     union SELECT topic3 as topic from feedbackform_db where topic3 is not null AND event_date between '$event_date1' AND '$event_date2' 
     union SELECT topic4 as topic from feedbackform_db where topic4 is not null AND event_date between '$event_date1' AND '$event_date2' 
     union SELECT topic5 as topic from feedbackform_db where topic5 is not null AND event_date between '$event_date1' AND '$event_date2' 
     union SELECT topic6 as topic from feedbackform_db where topic6 is not null AND event_date between '$event_date1' AND '$event_date2' 
     union SELECT topic7 as topic from feedbackform_db where topic7 is not null AND event_date between '$event_date1' AND '$event_date2' 
     union SELECT topic8 as topic from feedbackform_db where topic8 is not null AND event_date between '$event_date1' AND '$event_date2' "); 
相關問題