Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End sum column total from db

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #30949
    mbarandao
    Member

    Hello:

    I have a mysql table that stores customer transaction information. I want to sum column name “total” –which has values like this: $253.20– only where the value of another column is “mossa01”.

    For this, I passed the variable to the code via url and trimmed it. I then used the following statement:

    $client = @$_GET["q"] ;
    $trimmed = trim($client); //trim whitespace from the stored variable
    echo"$client";//equal mossa01
    $res=mysql_query('SELECT SUM(SUBSTRING(`total`,2)) AS `total` FROM `billofservice` where clientID like "$trimmed" ');

    //$result=mysql_query($res);

    if($res == false)
    {
    user_error("Query failed: " . mysql_error() . "
    n$res");
    }
    else
    if(mysql_num_rows($res) == 0)
    {
    echo "

    Sorry, no rows were returned by your query.

    n";
    }


    //if(!$res){
    // die("Error: ".mysql_error());
    //}

    $row = mysql_fetch_assoc($res);
    echo "Total income earned from this client to date is ";echo"$";echo($row);

    With this effort, I get the following error:

    Notice: Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1
    Resource id #3 in C:Apachehtdocsandyinvoicehistory.php on line 80

    May I request some assistance?
    Mossa

    #70160
    mbarandao
    Member

    in error! see below…

    #70152
    mbarandao
    Member

    I got the issue resolved with the following:

    $myString = "SELECT SUM(SUBSTRING(total,2)) AS total FROM billofservice where clientID like '%$trimmed%' ";
    $res = mysql_query($myString);

    if($res == false)
    {
    user_error("Query failed: " . mysql_error() . "
    n$res");
    }
    else
    if(mysql_num_rows($res) == 0)
    {
    echo "

    Sorry, no rows were returned by your query.

    n";
    }

    All is well!
    Mossa

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.