查询在PHP中返回空结果,否则工作正常

时间:2016-07-04 18:10:56

标签: php mysql mysqli phpmyadmin

这是sql查询,在phpmyadmin上测试时工作正常。 以下是截图。

SQL query working on phpmyadmin

下面是PHP代码及其HTML表单

<?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上运行它时,它是空的并且没有错误。 但是查询正在运行,因为它对响应变量

显示为“true”

这是结果

true {“details”:[]} Array()

为什么我在数组中没有得到任何值?

2 个答案:

答案 0 :(得分:1)

您的php代码无法复制phpmyadmin中的内容:escape()调用 NOT 为您引用值,因此您生成相当于

的内容
SELECT ... event_date between 2016-06-03 and 2016-06-08

请注意您的日期周围缺少报价。这意味着就MySQL而言,他们是 NOT 日期,他们是数学减法操作,并且你做的相当于:

SELECT ... event_date between 2007 and 2002

由于这是一个不可能实现的条件(between必须是smaller AND larger,而你有larger AND smaller),你得到0行,你仍然得到一个VALID结果句柄,最后得到一个空数组。

你应该

 $query = ("SELECT [...snip..] between '$event_date1' AND '$event_date2'
每个成员选择查询的

(请注意'引号)。

答案 1 :(得分: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' ");