• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

badly need help. php syntax

Newbie Spellweaver
Joined
Jul 7, 2005
Messages
20
Reaction score
1
im not php coder, i know litle on its basic.
its hard for me to print the $total and $remain. please correct the syntax im using,

$total=$donate+$suport;
$remain=$total-$expense;


this is the code and the liveview,
PHP:
    <?php
$donate1 = mssql_query("SELECT donate from Pos");
$donate= mssql_fetch_row($donate1);

$expense1 = mssql_query("SELECT expense from Pos");
$expense= mssql_fetch_row($expense1);

$support1 = mssql_query("SELECT support from Pos");
$support= mssql_fetch_row($support1);


$total=
$remain=



    ?>

    
<article class="rankingWrap">
<ul class="shadowz" style="width:200px;">
<li class="du">Server Funds/Expenses</li></ul>
<table width="100%" class="rankTable" align="center">
        <thead>
            <tr>
            <th width="20%"><b>Donate Funds</b></th>
	    
            <th width="20%"><b>Sponsor</b></th>
            
	    <th width="20%"><b>Total Funds</b></th>

	    <th width="20%"><b>Total Expenses</b></th>

            <th width="20%"><b>Remaining Funds</b></th>
            </tr>
        </thead>       
        <tbody>
        
        <?php



        echo'
<tr align="center">

<td>'.$donate[0].'</td>

<td>'.$support[0].'</td>

<td>'.$total[0].'</td>

<td>'.$expense[0].'</td>

<td>'.$remain[0].'</td>




</tbody></table></article>         

</tr>'; ?>




zarcmu - badly need help. php syntax - RaGEZONE Forums
 
Super-Moderator
Staff member
Super-Moderator
Joined
Apr 28, 2007
Messages
1,502
Reaction score
760
Your indentation is making my head explode. Anyway I'm pretty sure no matter what, mssql_fetch_row is always returning an array. Even when you're selecting only one column.

So $total would be:
$total = $donate[0] + $expense[0];

But you should really write your code more .. appropriate.. Replace your 3 queries with this:
PHP:
$query = mssql_query('SELECT donate, expense, support from Pos');
$result = mssql_fetch_row($query);


$donate = $row[0];
$expense = $row[1];
$support = $row[2];


$total = $donate + $support;
$remain = $total - $expense;

mssql_fetch_row will return an array with 3 indexes in the order you state in the query.
So $row[0] = column 'donate', $row[1] = column 'expense' and $row[2] = column 'support'.

Also, I didn't include it in my example, but make sure to check if the query ran successfully (if($query)..blabla) before using mssql_fetch_row.
 
Newbie Spellweaver
Joined
Jul 7, 2005
Messages
20
Reaction score
1
Your indentation is making my head explode. Anyway I'm pretty sure no matter what, mssql_fetch_row is always returning an array. Even when you're selecting only one column.

So $total would be:
$total = $donate[0] + $expense[0];

But you should really write your code more .. appropriate.. Replace your 3 queries with this:
PHP:
$query = mssql_query('SELECT donate, expense, support from Pos');
$result = mssql_fetch_row($query);


$donate = $row[0];
$expense = $row[1];
$support = $row[2];


$total = $donate + $support;
$remain = $total - $expense;

mssql_fetch_row will return an array with 3 indexes in the order you state in the query.
So $row[0] = column 'donate', $row[1] = column 'expense' and $row[2] = column 'support'.

Also, I didn't include it in my example, but make sure to check if the query ran successfully (if($query)..blabla) before using mssql_fetch_row.


thank you so much.you helped me a lot.

this is the new code.


PHP:
<?php 
$query = mssql_query("SELECT donate, expense, support from Pos"); 
$result= mssql_fetch_row($query); 

$total= $result[0]+$result[2];
$remain= $total-$result[1];



    ?> 

     
<article class="rankingWrap"> 
<ul class="shadowz" style="width:200px;"> 
<li class="du">Server Funds/Expenses</li></ul> 
<table width="100%" class="rankTable" align="center"> 
        <thead> 
            <tr> 
            <th width="20%"><b>Donate Funds</b></th> 
         
            <th width="20%"><b>Sponsor</b></th> 
             
        <th width="20%"><b>Total Funds</b></th> 

        <th width="20%"><b>Total Expenses</b></th> 

            <th width="20%"><b>Remaining Funds</b></th> 
            </tr> 
        </thead>        
        <tbody> 
         
        <?php 



        echo' 
<tr align="center"> 

<td>'.$result[0].'</td> 

<td>'.$result[2].'</td> 

<td>'.$total.'</td> 

<td>'.$result[1].'</td> 

<td>'.$remain'</td> 




</tbody></table></article>          

</tr>'; ?>


zarcmu - badly need help. php syntax - RaGEZONE Forums
 
Last edited:
Junior Spellweaver
Joined
May 14, 2008
Messages
161
Reaction score
65
It has been ages since I have posted, but I have recently started working with php as well,
and would just like to give a fair warning that the mssql_ functions become deprecated in PHP version 5.5, and removed in version 7.0.
If you ever get to such a point, sqlsrv_ is a great alternative!

Also, another cool feature PHP you can do is the following when grabbing from arrays;
Code:
$charsQ = sqlsrv_query($connsql, "SELECT * FROM [characters].[dbo].[characters] where accountName = '".getUser()."' order by lastloggeddate desc;");
while($chars = sqlsrv_fetch_object($charsQ)){ 
echo "
<option value=\"".$chars->characterName."\">".$chars->characterName."</option>";
}
notice the $chars->characterName

calling an array like that virtually allows you to call the name of the column from your query, instead of relying on positions~
 
Back
Top