运行mysqli查询时出现问题

时间:2016-04-29 07:35:20

标签: php mysql mysqli

Data Values of Database当我运行此查询时出现错误:

Table Structure

$sql_unique= "Select * from scandrug WHERE exp_dt == DATE_ADD(NOW(),INTERVAL 15 DAY)";
  

警告:mysqli_fetch_assoc()期望参数1为   mysqli_result,在 C:\ xampp \ htdocs \ qrcode \ date.php 中给出的布尔值   在线 28

我想从我的桌子中选择那些有效期还剩15天的记录? 请帮忙

以下是完整代码

 <?php
header('Content-type: text/plain');

$user = 'root';
$pass = '';
$con = 'pharmacy';

$con = new mysqli('127.0.0.1', $user, $pass, $con);

if(!$con)
{
echo 'Unable to Connect with Server';
}
if (!mysqli_select_db($con,'pharmacy'))
{
echo 'Database is Not Selected';
}


//select database
mysqli_select_db($con,'pharmacy');

$current_dt= date('d-m-Y');
$sql_unique= "Select * from scandrug WHERE exp_dt == DATE_ADD(NOW(),INTERVAL 15 DAY)";
$result = mysqli_query($con,$sql_unique);


while($row = mysqli_fetch_assoc($result))
{

   echo $row['name'];
   echo $row['batch_no'];
   echo "\n";
}


?>

1 个答案:

答案 0 :(得分:0)

假设exp_dt的数据类型为date,您应该运行此查询。

Select * 
from scandrug 
WHERE exp_dt = DATE(DATE_SUB(NOW(), INTERVAL 15 DAY))

DATE_SUB(NOW(), INTERVAL 15 DAY)将返回datetime。添加DATE只会返回date

修改

Select * 
from scandrug 
WHERE exp_dt = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 15 DAY), '%d-%m-%Y')

我使用DATE_FORMAT根据您使用的日期格式更改DATE_SUB返回的日期格式。