I am almost certain my query is being cached. It is a SELECT statement.
Here is an example situation. There are currently 2 pending orders. I have a script that displays a notification of pending orders on the home page to notify the warehouse if any new orders have been placed. I placed an order to test out if the “2” would update to “3” and it didn’t. I think my query is being cached, because when I run the query from phpMyAdmin it shows “3” but when ran through PHP it only shows “2”. Any ideas how to fix this or disable caching on specific queries?
Here was my original code
<?php
if( protectThis("1, 2") ) :
$pending = $conn->query("SELECT count(*) FROM orders WHERE `status` =0");
$pending2 = $pending->fetch();
$pendingcount = count($pending2);
if ($pendingcount > 0) {
?>
<div class="orange warning">
<p>
<strong>Pending Order Notification</strong>
<br />There are currently <strong><?php echo $pendingcount; ?></strong> pending orders.
<br /><a style="color:white;margin-top:10px;" class="btn btn-inverse" href="orders_pending.php"><i>Click here to view them.</i></a>
</p>
<div class="shadow-out"></div>
</div>
<?php
}
endif;
I tried altering the query after reading up on trying to disable the caching for specific queries and tried the following
SELECT SQL_NO_CACHE count(*) FROM orders WHERE `status` =0
Apparently that didn’t work either.
I’m not sure what to do. :\
Any help is much appreciated.
“EDIT 1”
$pending = $conn->query("SELECT count(*) FROM orders WHERE `status` =0");
// $pending2 = $pending->fetch();
// $pendingcount = count($pending2);
$pendingcount = $pending->rowCount();
if ($pendingcount > 0) {
I think you are counting the wrong way. Try
var_dump($pending2). I guess you’ll get this output:$pending2is an array containing the only row your query returned, with the only column indexed by both column name and 0-indexed column number. Any single-row resultset will look like this, and if youcountit, it will always output2.So instead of this:
you should use this: