-1

I am using PHP(PDO), MYSQL. I have a number of date values stored in my database and am trying to compare my system date with date values stored in the database one by one.

I can compare a single date value, but I am not able to compare all the date values. I want to compare all the date values from the database with my system date and if it matches it should echo.

I know it would be under loop concept, but I don't know how to run loop on that. Is this the correct concept or what?

 $sysdate= date("Y-m-d");//system date;

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM setremainder"); 
$stmt->execute();
$result=$stmt->fetch();
$r1=$result['datetime'];

comparing systemdate with datbase value

 if ($r1==$sysdate) {
    echo success";
}
1

1 Answer 1

1

How about using WHERE clause (it will also be faster), which will return only matched records.

$sysdate= date("Y-m-d");//system date;

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn->prepare("SELECT * FROM setremainder where datetime = :datetime");
$stmt->execute(["datetime" => $sysdate]);
// Above 2 line will return records that only matches the record
foreach ($stmt as $row) {
    echo "success";
}

And since you mentioned you only need to print something when it is matched with records perhaps, you can also use COUNT() function that will only count number of rows that matches your statement with given system date.

$sysdate= date("Y-m-d");//system date;

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn->prepare("SELECT count(*) FROM setremainder where datetime = :datetime");
$stmt->execute(["datetime" => $sysdate]);
// Above 2 line will return records that only matches the record
$number_of_rows = $stmt->fetchColumn();

NOTE:

Make sure your DB column date format and extracted system date param are either same or compatible

Sign up to request clarification or add additional context in comments.

1 Comment

@arun if my answer solved your problem, consider marking it your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.