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.

Recommended Answers

All 6 Replies

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'] ;  
}  
?> 

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,

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");

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.

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, :)

echo $row['brick'];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.