Results 1 to 3 of 3

Thread: Echo and calculate data from db

  1. #1
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Echo and calculate data from db

    Hello,

    I am trying to echo results from mysql.

    I have a table 'rb_pallet_record' and a column 'quantity' which records the amount of products.

    What I would like to do is calculate all of the quantity data echoing the total.

    ie.

    quantity

    5
    8
    7
    5

    total = 25

    here is the php i am working with

    PHP Code:
    <?php $link mysql_connect('localhost''root''taycon');

    if (!
    $link) {    die('Could not connect: ' mysql_error());}echo '';mysql_select_db("rb");?>



    <?php
    $sql 
    "select count(quantity) from rb_pallet_record";
    $result=mysql_query($sql,$link)or die(mysql_error());
    $row mysql_fetch_array($resultMYSQL_NUM); 
    echo 
    $row[0]; 
    ?>
    Thanks
    Last edited by john1991; 05-11-2014 at 01:34 PM.

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    This should be a simple change.
    PHP Code:
    $sql "SELECT SUM(`quantity`) as `total` FROM `rb_pallet_record`"// You really should capitalize the key words in a sql string to make it more readable.  Plus surrounding your table names and column names with `backticks` will make sure the query doesn't accidentally think those words mean something else besides table and column names.
    $result=mysql_query($sql)or die(mysql_error()); // There is no need for the $link in the query call with the basic mysql_query.
    $row mysql_fetch_assoc($result); // The assoc array is easier to use and does not contain the extra indexed array.  Also the MYSQL_NUM is not needed, I've never actually seen that used. 
    echo $row['total']; // Output as 'total' since that is what it's not referenced as in the query. 
    As a last side note, you really should stop using the mysql functions as they are now deprecated and will be removed in the future from PHP. You should be using either mysqli or PDO which is the more preferred method.
    You never know everything, I learn everyday!

  3. The Following User Says Thank You to fastsol1 For This Useful Post:

    john1991 (05-11-2014)

  4. #3
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thank you for your help and pointers definitely a learning curve!

Similar Threads

  1. Replies: 2
    Last Post: 03-17-2013, 04:27 PM
  2. Resolved php echo data from db
    By john0611 in forum PHP
    Replies: 2
    Last Post: 11-30-2009, 10:53 PM
  3. Resolved php echo data
    By john0611 in forum PHP
    Replies: 2
    Last Post: 03-13-2009, 09:46 PM
  4. Replies: 2
    Last Post: 11-30-2008, 07:32 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •