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
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...
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.