Jump to content

Why Does my PHP miss a value from my DB ?


vincej

Recommended Posts

I am trying to find the order values between 2 dates: $date1 and $date2. My code appears to work BUT, for some reason it always leaves out one of the values from my MySQL DB table. I have checked in the table that the Time value of the order being left out is indeed within the date range and yup it sure it, but for some crazy reason it is either not being picked up by the sql statement or the PHP statement or something -  I just don't get it > there must be a stupid error somewhere

 

Many Many Thanks for your help - Vincej

 

 

 

 


/* Create Query*/
<?php
$query_sales_shipping_tax = "SELECT order_id, user_id, order_tax, order_shipping_tax, cdate FROM jos_vm_orders ORDER BY cdate DESC";
$sales_shipping_tax = mysql_query($query_sales_shipping_tax, $teashop) or die(mysql_error());
$row_sales_shipping_tax = mysql_fetch_assoc($sales_shipping_tax);
?>

/*Create dates for start and end of range 	*/	
<?php
$date1 = strtotime("09/30/2010");
$date2 = strtotime("10/05/2010");
?>
<br />


/*Create While statement Create Conditions & Echo results*/ 

<?php
while ($row_sales_shipping_tax = mysql_fetch_assoc($sales_shipping_tax)){
if ($row_sales_shipping_tax['cdate']  >$date1 && $row_sales_shipping_tax['cdate']  <$date2){?>

		<tr>
            <td><?php echo $row_sales_shipping_tax['order_id']; ?></td>
            <td><?php echo $row_sales_shipping_tax['user_id']; ?></td>
            <td><?php echo strftime("%m/%d/%Y/%H/%M/%S", $row_sales_shipping_tax['cdate']); ?></td>
            <td><?php echo $row_sales_shipping_tax['order_tax']; ?></td>
            <td><?php echo $row_sales_shipping_tax['order_shipping_tax']; ?></td>
            </tr>


<?php }	

 

 

Link to comment
https://forums.phpfreaks.com/topic/226416-why-does-my-php-miss-a-value-from-my-db/
Share on other sites

because you do this once, before doing it again within a loop:

 

$row_sales_shipping_tax = mysql_fetch_assoc($sales_shipping_tax); // pull off first row of data

// ... then later
while ($row_sales_shipping_tax = mysql_fetch_assoc($sales_shipping_tax)){ // starts with second row

Dreamweaver is notorious for doing this.

 

It is as BlueSky states.  You are calling mysql_fetch_assoc which increments your array data pointer by 1, then you follow it up later with another call.  This second call doesn't reset the data pointer.  So you miss your first row of data.  Take out the first data call, as it is not needed or used.

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.