Jump to content

MYSQL update query in PHP if statement?


eMonk

Recommended Posts

This isn't the entire code just enough to see what I'm trying to do.

 

Everything was working until I added the mysql update query in the if statement. Is this possible or am I doing something wrong?

 

When I run the script it just echos "No results found" twice as $num_results = 2. 

 

<?php

include("../includes/connect.php");

$query = "SELECT ........ ";
$result = $db->query($query);

$num_results = $result->num_rows;

if ($num_results == 0) {

exit;

} else {

$i=0;
while ($i < $num_results) {
$row = $result->fetch_assoc();

$id = $row['id'];

if ($expiration_date > $today) {

### EMAIL CODE HERE ###

$update = "UPDATE model SET reminder_sent = '1' WHERE id = '$id' ";
$result_2 = $db->query($update);

$i++;

} else {

echo "No results found.";

$i++;

}
}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/251581-mysql-update-query-in-php-if-statement/
Share on other sites

The "No results found" shows when $expiration_date is lower than $today, right? That means that it doesn't even go to the UPDATE code.

 

What database class you're using? Anyway, the idea would be to loop through the results, or you'll get only the first row. In each loop iteration, calling $result->fetch_assoc() should bring up the same row. Normally, you should have:

 

<?php
while ($rows = $result->fetch_assoc()) {
     $id = $row['id'];
     //etc...
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.