fantomel Posted January 25, 2009 Share Posted January 25, 2009 hello first of all sorry for my english and my writing i'm a little in hurry cuz i have to get some sleep i have a simple small question. This is my first time working with php maths and i need to do a script that will do something like this: 1. insert a number like 25000 in a table and when i hit a button to show all the data from db i want to make a total. ex: 25+25 = 50 -> it's about money and i want to see a total after i hit show report... can someone help me with an explanation ..? Quote Link to comment https://forums.phpfreaks.com/topic/142391-solved-mysql-php-maths/ Share on other sites More sharing options...
Mchl Posted January 25, 2009 Share Posted January 25, 2009 You want to sum all rows returned by SELECT query? SELECT SUM(column) FROM table WHERE... Quote Link to comment https://forums.phpfreaks.com/topic/142391-solved-mysql-php-maths/#findComment-746050 Share on other sites More sharing options...
fantomel Posted January 25, 2009 Author Share Posted January 25, 2009 thank you for your reply this is what i want to do: CREATE TABLE IF NOT EXISTS `internet_hours` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `price` varchar(70) NOT NULL, `how_many_hours` varchar(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; i work at internet cafe and i want to know how much money i got how many hours have the clients bought because now we have some sodas and stuff for sale.. and i wanna know exacly how when where.. ) it's a personal project for the moment all i want to do is to insert the price of an hour or half an hour in the db and then get the total of the sum i have there -- -- Table structure for table `internet_hours` -- CREATE TABLE IF NOT EXISTS `internet_hours` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `price` varchar(70) NOT NULL, `how_many_hours` varchar(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; -- -- Dumping data for table `internet_hours` -- INSERT INTO `internet_hours` (`id`, `price`, `how_many_hours`) VALUES (1, '25000', '1'), (2, '25000', '1'); as you see i've inserted some example datas:) and then on my page i will hit show report and then show me a page with all the data for example or only the total sum of the money Quote Link to comment https://forums.phpfreaks.com/topic/142391-solved-mysql-php-maths/#findComment-746056 Share on other sites More sharing options...
Mchl Posted January 25, 2009 Share Posted January 25, 2009 Do not store money as VARCHAR Use DECIMAL column for that Quote Link to comment https://forums.phpfreaks.com/topic/142391-solved-mysql-php-maths/#findComment-746058 Share on other sites More sharing options...
fantomel Posted January 25, 2009 Author Share Posted January 25, 2009 aham i'm a beginer in php .. and php math it's very new to me .. including most of mysql commands i learn .. when a i do something new just for testing or personal. after you'v seen the table is it corect your first post ? to use this command : SELECT SUM(column) FROM table WHERE... Quote Link to comment https://forums.phpfreaks.com/topic/142391-solved-mysql-php-maths/#findComment-746065 Share on other sites More sharing options...
Mchl Posted January 25, 2009 Share Posted January 25, 2009 It will be SELECT SUM(price) FROM internet_hours but you HAVE TO change price column to DECIMAL type (or if in your country, prices with decimal places are _extremely_ unlikely, you could use INTEGER - I would advice using DECIMAL with two places after point though) [edit] Oh... wait a second. Just realised, you probably want price * how_many_hours SELECT SUM(price * how_many_hours) FROM internet_hours And of course you should change a type of 'how_many_hours' to a numeric type as well. Quote Link to comment https://forums.phpfreaks.com/topic/142391-solved-mysql-php-maths/#findComment-746072 Share on other sites More sharing options...
fantomel Posted January 25, 2009 Author Share Posted January 25, 2009 i don't know exacly how should i translate decimal in my language but i think you are refering to prices writen like this : 25,000 or 25.000 in my country we use both methods Quote Link to comment https://forums.phpfreaks.com/topic/142391-solved-mysql-php-maths/#findComment-746073 Share on other sites More sharing options...
fantomel Posted January 25, 2009 Author Share Posted January 25, 2009 -- -- Table structure for table `internet_hours` -- CREATE TABLE IF NOT EXISTS `internet_hours` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `price` decimal(10,0) NOT NULL, `how_many_hours` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; -- -- Dumping data for table `internet_hours` -- INSERT INTO `internet_hours` (`id`, `price`, `how_many_hours`) VALUES (1, '25000', 1), (2, '25000', 1); i've made the changes Quote Link to comment https://forums.phpfreaks.com/topic/142391-solved-mysql-php-maths/#findComment-746076 Share on other sites More sharing options...
fantomel Posted January 25, 2009 Author Share Posted January 25, 2009 problem solved thank you for your help in such short time i will post tomorrow morning a full script for this project maybe someone else will need it or ..just for an example how things work thanks again Quote Link to comment https://forums.phpfreaks.com/topic/142391-solved-mysql-php-maths/#findComment-746082 Share on other sites More sharing options...
Mchl Posted January 25, 2009 Share Posted January 25, 2009 i will post tomorrow morning a full script for this project maybe someone else will need it or ..just for an example how things work That's a great idea. Please do! Quote Link to comment https://forums.phpfreaks.com/topic/142391-solved-mysql-php-maths/#findComment-746109 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.