Jump to content

quick help with adding together values from table....


seany123

Recommended Posts

i have for example this table....

 

-- phpMyAdmin SQL Dump

-- version 3.2.0.1

-- http://www.phpmyadmin.net

--

-- Host: localhost

-- Generation Time: Nov 12, 2009 at 04:33 PM

-- Server version: 5.1.37

-- PHP Version: 5.3.0

 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

 

--

-- Database: `maf10000_db1`

--

 

-- --------------------------------------------------------

 

--

-- Table structure for table `items`

--

 

CREATE TABLE IF NOT EXISTS `items` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `player_id` int(11) NOT NULL DEFAULT '0',

  `item_id` int(11) NOT NULL DEFAULT '0',

  `type` enum('weapon','armour','shoe','consumable','pet','rare','parts','other') COLLATE latin1_general_ci NOT NULL,

  `quantity` int(11) NOT NULL,

  `status` enum('equipped','unequipped') COLLATE latin1_general_ci NOT NULL DEFAULT 'unequipped',

  PRIMARY KEY (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=27 ;

 

--

-- Dumping data for table `items`

--

 

INSERT INTO `items` (`id`, `player_id`, `item_id`, `type`, `quantity`, `status`) VALUES

(26, 1, 96, 'rare', 1, 'unequipped'),

(25, 1, 8, 'armour', 5, 'equipped'),

(24, 1, 6, 'weapon', 1, 'equipped'),

(23, 1, 5, 'weapon', 12, 'unequipped'),

(27, 5, 5, 'weapon', 1, 'equipped');

 

 

now what i wanna do is SELECT * from that table where player_id = $player->id...

 

THEN add all the quanities together.

 

currently have this code but dont know how to do the quantity bit:

 

$query = $db->execute("SELECT * FROM items WHERE player_id='$player->id'");
$item = $query->fetchrow();

echo "Item Quantity here: ".$item['quantity']."<br><br>";

Link to comment
Share on other sites

You mean...

 

SELECT SUM(quantity) FROM items WHERE player_id = $player->id

 

?

 

okay so i did this...

 

$rcount = $db->execute("SELECT SUM(quantity) FROM items WHERE player_id = '$player->id'");

echo "Inventory Usage:".$rcount."/250";

 

but its echoing: Inventory Usage:SUM(quantity) 19 /250

 

however the 19 is correct... i just need to know how to remove the SUM(quantity) from the echo.

Link to comment
Share on other sites

Firstly I should have probably names given it an alias in my last example...

 

SELECT SUM(quantity) as quant FROM items WHERE player_id = $player->id

 

Secondly, I've not really used mysqli much, but I'm guessing you need to fetch row, not just echo the result of the execute.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.