Forum Moderators: coopster

Message Too Old, No Replies

Date Formating PhP MySQL

It worked before...

         

theriddla1019

3:45 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Here is the code! :P

<?
$Query = "Select *";
$Query = $Query . ", DATE_FORMAT(eFrom , '%m/%d/%Y') as eFrom";
$Query = $Query . ", DATE_FORMAT(Thru , '%m/%d/%Y') as Thru";
$Query = $Query . ", DATE_FORMAT(FirstBillable , '%m/%d/%Y') as FirstBillable";
$Query = $Query . " FROM episodedata WHERE MR = '" . $MR . "' ORDER BY 'eFrom' DESC";
$Result = mysql_query($Query,$Link);
$Num = mysql_numrows($Result);

{
$i=0;
while ($i < $Num)
{
$eFrom=mysql_result($Result,$i, 'eFrom');
$Thru=mysql_result($Result,$i,"Thru");
$Status=mysql_result($Result,$i, "Status");
$Firstb=mysql_result($Result,$i,"FirstBillable");
$ID=mysql_result($Result,$i,"EpisodeID");
++$i;
?>
<tr>
<td width="150" class="List"><center><a href="episode_detail.php?EpisodeID=<?= $ID?>"><?= $eFrom?></a></center></td>
<td width="150" class="List"><center><a href="episode_detail.php?EpisodeID=<?= $ID?>"><?= $Thru?></a></center></td>
<td width="150" class="List"><center><?= $Firstb;?></center></td>
<td width="150" class="List"><center><?= $Status;?></center></td>
</tr>
<?
if ($i < $Num) {
$eFrom=mysql_result($Result,$i,"eFrom");
$Thru=mysql_result($Result,$i,"Thru");
$Status=mysql_result($Result,$i, "Status");
$Firstb=mysql_result($Result,$i,"FirstBillable");
$ID=mysql_result($Result,$i,"EpisodeID");
?>
<tr>
<td width="150" class="List2"><center><a href="episode_detail.php?EpisodeID=<?= $ID?>"><?= $eFrom?></a></center></td>
<td width="150" class="List2"><center><a href="episode_detail.php?EpisodeID=<?= $ID?>"><?= $Thru?></a></center></td>
<td width="150" class="List2"><center><?= $Firstb;?></center></td>
<td width="150" class="List2"><center><?= $Status;?></center></td>
</td>
</tr>
<?
++$i;
}
}
}
?>

Here is the problem!:
Normaly when i DATE_FORMAT() the sql query it formats my date with the arguments, it works on all other 15 of my pages but for some reason it just wont work here. The only difference between this page and all the other pages is the fact that im using the MYSQL_NUMROWS and then instead of $Variable=$Row['Var'] im using the $Variable=mysql_result($Result,$i,"Var"); Is the way im doing this wrong? I would think that the select would pull all the data and the numrows only counts the rows so i can pull the number to use my while statement. I wouldnt think it would affect the formating of my data. But it just returns the values in the standard mysql format with no formatting at all.

ive tried

$Thru=mysql_result($Result,$i,"Thru");
$ThruForm=date(m d Y, $Thru);

and it didnt like that, it did the same thing
so anyone got any ideas?
or should i just rebuild my statement to be mysql_fetch_assoc and do my loop like that?
(that would mean alot of searchin of the internet :P)
Thanks and Help!

Timotheos

1:22 am on Feb 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hiya Riddla

Select *, DATE_FORMAT(eFrom , '%m/%d/%Y') as eFrom ...

You say this works on other pages put I'm just wondering if there might be a conflict with using the * in your select statement. I mean you've selected the 'eFrom' column with * and then you're doing it again with the date format. If you don't need to select everything then don't use the *.

As for mysql_fetch_assoc, I'd think that would be a wise investment to get up to speed on that function.

theriddla1019

2:05 pm on Mar 1, 2004 (gmt 0)

10+ Year Member



Well when you do the selection * and then Date_Format your just telling it when you get to the data in the row eFrom format it as a date, its not another selection row. But i guess i will have to find a way to run my loop using fetch_assoc. I use fetch_assoc in anything im not looping just not sure how to use it in a loop statement.

Timotheos

4:27 pm on Mar 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you'll find that your result set has two columns name eFrom. One that comes from SELECT * and the other from the DATE_FORMAT (blah, blah) as eFrom.


eFrom......eFrom
2004-01-07 01/07/2004
2004-01-07 01/07/2004

So when you use mysql_result($Result,$i, 'eFrom') it is grabbing the first column named eFrom which is unformatted.

Why not use a different alias to prove it?
SELECT *, DATE_FORMAT( `eFrom` , '%m/%d/%Y' ) AS eFromFormatted FROM episodedata

coopster

4:53 pm on Mar 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Timotheos is correct in that there will be two columns returned with the same associative index. PHP doesn't "replace" the original.

However, for the record, if two or more columns of the result have the same field names, the last column will take precedence, not the first. See mysql_fetch_assoc [php.net] and mysql_fetch_array [php.net] for details.

theriddla1019

5:19 pm on Mar 1, 2004 (gmt 0)

10+ Year Member



actually i did replace it with a new "as eFromForm"
and it gave the same results so i just changed it back
as to not get so frazzled with 10000 different variables

i did a little foolin round and searchin and couldnt find anything so i decided to figure it out myself again. This time it worked! heres what i came up with...probably not the prettiest thing but it works :)

<?
$Query = "Select *";
$Query = $Query . ", DATE_FORMAT(eFrom , '%m/%d/%Y') as eFrom";
$Query = $Query . ", DATE_FORMAT(Thru , '%m/%d/%Y') as Thru";
$Query = $Query . ", DATE_FORMAT(FirstBillable , '%m/%d/%Y') as FirstBillable";
$Query = $Query . " FROM episodedata WHERE MR = '" . $MR . "' ORDER BY 'eFrom' DESC";
$Result = mysql_query($Query,$Link);
$Num = mysql_numrows($Result);
$Message = "There are $Num episodes for patient $First $Last.";
if ($Num == 0)
{
$Message = "There are no episodes for this patient.";
} else {
$i=0;
while ($Row = mysql_fetch_assoc($Result)) {
if ($i == 0) {

?>
<tr>
<td width="150" class="List"><center><a href="episode_detail.php?EpisodeID=<?= $Row['EpisodeID']?>&MR=<?= $Row['MR']?>"><?= $Row['eFrom']?></a></center></td>
<td width="150" class="List"><center><a href="episode_detail.php?EpisodeID=<?= $ID?>&MR=<?= $MR?>"><?= $Row['Thru']?></a></center></td>
<td width="150" class="List"><center><?= $Row['FirstBillable']?></center></td>
<td width="150" class="List"><center><?= $Row['Status']?></center></td>
</tr>
<?
$i=1;
} else {
?>
<tr>
<td width="150" class="List2"><center><a href="episode_detail.php?EpisodeID=<?= $Row['EpisodeID']?>&MR=<?= $Row['MR']?>"><?= $Row['eFrom']?></a></center></td>
<td width="150" class="List2"><center><a href="episode_detail.php?EpisodeID=<?= $ID?>&MR=<?= $MR?>"><?= $Row['Thru']?></a></center></td>
<td width="150" class="List2"><center><?= $Row['FirstBillable']?></center></td>
<td width="150" class="List2"><center><?= $Row['Status']?></center></td>
</td>
</tr>
<?
$i=0;
}
}
}
?>

The major hurdle i had was that every other line in the background was highlighted in a different color as to seperate the search results...and the only way i knew to loop like that was to use numrows... but this simple and obvious solution evaded me..but everything works great!