Mirza Faizan Abid Mirza Faizan Abid - 7 months ago 19
SQL Question

Issue with running the mysqli query

Data Values of DatabaseWhen i run this query i got error:

Table Structure

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



Warning: mysqli_fetch_assoc() expects parameter 1 to be
mysqli_result, boolean given in C:\xampp\htdocs\qrcode\date.php
on line 28



I want to select those records from my table which have 15 days left from expiry?
please help

Here is full code

<?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";
}


?>

A J A J
Answer

Assuming data type of exp_dt is date, you should run this query.

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

DATE_SUB(NOW(), INTERVAL 15 DAY) will return datetime. Adding DATE will return only date.

EDIT

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

I used DATE_FORMAT to change format of date returned by DATE_SUB according to the date format you are using.

Comments