BetaONE will rise again!

BetaONE will rise again! (http:\\b1.hcanet.com\forum/index.php)
-   Web Development (http:\\b1.hcanet.com\forum/forumdisplay.php?f=50)
-   -   Trouble With Holding Mysql Query Results (http:\\b1.hcanet.com\forum/showthread.php?t=10435)

JacKDynne 22nd Jan 04 02:56 PM

I am having trouble holding the result of a query in a var then having it print out later, here is the code:

Code:

<?
//Set variable for sum1
        $sum1 = 0;
?>

<?
       
        // get current user sum of straight time
        $query = "SELECT SUM(hours) from log WHERE tid = '1' AND date = '$sdate' AND date = '$edate' AND uid = '$_SESSION[SESSION_UID]'";
        $result = mysql_db_query($database, $query, $connection) or die ("Error in query: $query . " . mysql_error());
       
        // Fetch first sum and hold in a var
        while ($sum1 = mysql_fetch_array($result))

 &nbsp;
?>

<!-- spacer -->
<tr>
<td colspan=2>&nbsp;</td>
</tr>

<tr>
<td colspan=2 valign="top" align="left">
<b><font color="#3098C3">&nbsp;&nbsp;Task/Hours Summary:</font></b>
</td>
</tr>

<tr>
<td valign="top" align="left">
        <!-- tasks/hours summary &nbsp;-->
        <table border="0" cellspacing="2" cellpadding="5">
        <tr>
        <td valign=top align=left><font color=#D03468>Task</font></td>
        <td valign=top align=center><font color=#D03468>Hours</td>
        </tr>
        <tr>
        <td valign=top align=left><font color=#000000>Regular Hours</font><td valign=top align=center><font color=#000000><b><? echo print($sum1); ?></b></td>
        </tr>
        </table>

It keeps returning the array value as the result of the query, not the actual sum. I have run the query directly against the db in phpmyadmin and it returns the right value. Any ideas?

btw - please ignore the sloppy code, I have been cutting this script up all over the place :D

Mucho TIA :)

/JD
---------------------------------------------------------------------------------------------
EDIT - Making some progress... added/changed some code - now it seems that my query is not running right or somehow not returning the same value as when I run it straight against the db :(
Code:

<?
       
        // get current user sum of straight time
        $query = "SELECT SUM(hours) from log WHERE tid = '1' AND date = '$sdate' AND date = '$edate' AND uid = '$_SESSION[SESSION_UID]'";
        $result = mysql_db_query($database, $query, $connection) or die ("Error in query: $query . " . mysql_error());
       
        // Fetch first sum and hold in a var
        while ($sum1 = mysql_fetch_row($result));
       
        // correction if sum is zero - explicitly assign it 0, so that it gets printed in report
 &nbsp; &nbsp;if (!$sum1) { $sum1 = 0; }

 &nbsp;
?>

<!-- spacer -->
<tr>
<td colspan=2>&nbsp;</td>
</tr>

<tr>
<td colspan=2 valign="top" align="left">
<b><font color="#3098C3">&nbsp;&nbsp;Task/Hours Summary:</font></b>
</td>
</tr>

<tr>
<td valign="top" align="left">
        <!-- tasks/hours summary &nbsp;-->
        <table border="0" cellspacing="2" cellpadding="5">
        <tr>
        <td valign=top align=left><font color=#D03468>Task</font></td>
        <td valign=top align=center><font color=#D03468>Hours</td>
        </tr>
        <tr>
        <td valign=top align=left><font color=#000000>Regular Hours</font>
 &nbsp;<td valign=top align=center><font color=#000000><b><? echo sprintf("%1.01f", $sum1); ?></b></td>
        </tr>
        </table>

/me keeps trying :)
----------------------------------------------------------------------------------------------
Edit Again - Seems the result is where my fault is - not sure what I am doing wrong - will keep trying:)
----------------------------------------------------------------------------------------------

JacKDynne 27th Jan 04 08:11 PM

Resolved :)

This is how:

Code:

<?
        // get current user sum of straight time
        $query = "SELECT SUM(hours) from log WHERE tid = '1' AND date >= '$sdate' AND date <= '$edate' AND uid = '$_SESSION[SESSION_UID]'";
        $result = mysql_db_query($database, $query, $connection) or die ("Error in query: $query . " . mysql_error());
        $reghrs = mysql_result($result, 0);
       
        // correction if sum is zero - explicitly assign it 0, so that it gets printed in report
        if (!$reghrs) { $reghrs = 0; }
?>

and this to print it:

Code:

<tr>
        <td valign=top align=left><font color=#000000>Regular Hours</font>
 &nbsp;
        <td valign=top align=center><font color=#000000><b><? echo sprintf("%1.01f", $reghrs); ?></b></td>
        </tr>

Props out to Sephiroth :D

/JD


All times are GMT +1. The time now is 10:07 PM.

Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.