0

hi I am having trouble on echoing a sum from a table,

The mysqul works and give me the wantted result and looks like

SELECT sum(brick) FROM `goodship` 

this is the code i am trying to get to show it on the site,

    <html>
    <head>
    <?php
    $con=mysqli_connect("hostwiththemost.com","bulderbob","superscreatpassword","shipstable");
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    ?>  
    </head>  
    <body>  
    <?php  
    $result = mysql_query($con"SELECT SUM(brick) FROM goodship");  
    while($row=mysql_fetch_array($result))  
    {  

    echo  $row['SUM(brick)'] ;  

    }  
    ?> 
    </body>
    </html>

any see why this is not working ?
thanks.

3
Contributors
6
Replies
24
Views
3 Years
Discussion Span
Last Post by kgavhane12
0

There is no column called SUM(brick). You need to set the column name in the query after the column sum.

<?php  
$result = mysql_query($con"SELECT SUM(brick) AS 'brick' FROM goodship");  
while($row=mysql_fetch_array($result))
{  
    echo  $row['brick'] ;  
}  
?> 
0

thanks pielsoul ,

<?php
$result = mysql_query($con"SELECT SUM(brick) AS 'brick' FROM goodship");
while($row=mysql_fetch_array($result))

> {  
echo  $row['brick'] ;  
> }  

?>
this is still returning the error

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/a1732732/public_html/stockyard.php on line 15

I have been adding () and . in trial and error and still looping back to above,

1

Here is a problem

$result = mysql_query($con"SELECT SUM(brick) AS 'brick' FROM goodship");

just make it

$result = mysqli_query($con, "SELECT SUM(brick) AS 'brick' FROM goodship");

Edited by pixelsoul

1

Oops, you also missed the while statement

while($row=mysql_fetch_array($result))

should be

while($row=mysqli_fetch_array($result))

Be careful that all of these are using mysqli rather than mysql.

Edited by pixelsoul

0

THANKS pixelsoul , i had no idea what i was using, just it was not computing the way i was asking it to ,
it is now, :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.