如何在PHP中进行查询以搜索开始日期和结束日期

时间:2019-03-27 02:41:38

标签: php oracle

我无法将此查询从Oracle数据库转换为PHP。

<input type="date" name="stardate" value="startdate" id="startdate" value="<?php echo isset($_GET["startdate"]);?>"> 
<input type="date" name="enddate" value="enddate" id="enddate" value="<?php echo isset($_GET["enddate"]);?>">

这是我的正确查询,但我不知道如何将其放入PHP:

SELECT *
  FROM datepick
 WHERE start_date not between to_date('03/28/2019','mm/dd/yyyy') and to_date('03/30/2019','mm/dd/yyyy')
 and end_date not between to_date('03/28/2019','mm/dd/yyyy') and to_date('03/30/2019','mm/dd/yyyy');

1 个答案:

答案 0 :(得分:0)

提交表单后,获取变量中的日期并使用mysqli或pdo建立php-mysql连接,然后通过串联,可以将这些值传递给mysql查询。

<?php
// support you done form submit with get/post

$stardate = $_REQUEST['stardate']; // 03/28/2019
$enddate = $_REQUEST['enddate']; // 03/30/2019

// mysqli connection
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT * FROM datepick WHERE 
start_date not between to_date('".$stardate."','mm/dd/yyyy') and to_date('".$enddate."','mm/dd/yyyy')
and end_date not between to_date('".$stardate."','mm/dd/yyyy') and to_date('".$enddate."','mm/dd/yyyy')";
$result=mysqli_query($con,$sql);

// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
print_r ($row);
// check output and modify code accordingly
?>