eMonk Posted November 22, 2011 Share Posted November 22, 2011 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++; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/251581-mysql-update-query-in-php-if-statement/ Share on other sites More sharing options...
Fadion Posted November 22, 2011 Share Posted November 22, 2011 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... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/251581-mysql-update-query-in-php-if-statement/#findComment-1290239 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.