seany123 Posted November 12, 2009 Share Posted November 12, 2009 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 https://forums.phpfreaks.com/topic/181257-quick-help-with-adding-together-values-from-table/ Share on other sites More sharing options...
cags Posted November 12, 2009 Share Posted November 12, 2009 You mean... SELECT SUM(quantity) FROM items WHERE player_id = $player->id ? Link to comment https://forums.phpfreaks.com/topic/181257-quick-help-with-adding-together-values-from-table/#findComment-956207 Share on other sites More sharing options...
seany123 Posted November 12, 2009 Author Share Posted November 12, 2009 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 https://forums.phpfreaks.com/topic/181257-quick-help-with-adding-together-values-from-table/#findComment-956210 Share on other sites More sharing options...
cags Posted November 12, 2009 Share Posted November 12, 2009 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 https://forums.phpfreaks.com/topic/181257-quick-help-with-adding-together-values-from-table/#findComment-956222 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.