View Full Version : Update mysql Data after query in a while loop?

08-30-2011, 03:00 PM
I'm trying to get this round my head, but i am failing quite hard :o

I have rows in a database which i am echoing out in a while() loop, however i want to use a switch statement to change the output to something more readable please see code example below.

//Fetch all needed records
$strQuery ="SELECT * FROM lead_partners_pages ";
$strQuery .="WHERE afid_seller = '202'"; //$password
$result = mysql_query($strQuery) or die(mysql_error());

$query2 = "SELECT SUM(price_CIQFY_paid) AS totalPrice ";
$query2 .="FROM lead_partners_pages ";
$query2 .="WHERE afid_seller = '202'"; //$password
$result_set2 = mysql_query($query2);

if ($result && $result_set2) {
while($ors = mysql_fetch_array($result) AND $ors2 = mysql_fetch_array($result_set2)) {
switch ($ors['date_month'])
case 01:
echo "Jan";
case 02:
echo "Feb";
case 03:
echo "Mar";
case 04:
echo "Apr";
case 05:
echo "May";
case 06:
echo "Jun";
case 07:
echo "Jul";
case 08:
echo "Aug";
case 09:
echo "Sep";
case 10:
echo "Oct";
case 11:
echo "Nov";
case 12:
echo "Dec";
echo "No number between 01 and 12";

$strXML .= "<set name='{$ors['date_month']}' value='{$ors2['totalPrice']}' />";

08-30-2011, 03:37 PM
Okay and the problem is?

Wait, I see it. You cannot use 01... as your numbers. These are octal numbers and will not likely compare properly to the switch beyond the 8th month. You will need to convert these to strings to use them, or use an integer datatype in your database.

08-30-2011, 03:45 PM
The problem is, you're checking for integers. Anything coming from a database will be a string until you change it otherwise (I think). So, wrap your case check's with ' to compare your switch for a string, and it should work :).

A better way of implementing dates is to use a timestamp from time() stored in the database. That way, you can use the date() function to pull any information you want from it - including time. It's also makes comparison much easier. You should look into that :). With a timestamp, you wouldn't need a switch statement - all you would do is date( "M", $timestamp) and that would give you the short month. http://php.net/manual/en/function.date.php - more info there

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum