Stats: 3,192,337 members, 7,947,527 topics. Date: Thursday, 12 September 2024 at 10:11 PM |
Nairaland Forum / Science/Technology / Programming / Pls Help Me With This Php Code (1996 Views)
This Php Is About To Make Me Go Nuts. Pls Help / My Php Code Does Not Execute But Displays Code On Browser, / What Could Be Wrong With This Php Code? (2) (3) (4)
Pls Help Me With This Php Code by lillylove2(f): 7:50pm On Jan 21, 2016 |
Hi NL Programmers, pls this php code is running me mad. I really need your help. In my database Table, I have 2 rows. The first is named 'User' and d second is named 'amount'. There are two records in the table: Record 1: User=dami24, Amount=120 Record 2: User=dami24, Amount=30 What im trying to do is to "Select Amount From Table Where user=dami24" After selecting the amounts, i want to add them together, so the final result should be 150 (120+30). l've tried all i could without the desired result. Array_sum, implode/explode, mysql-fetch_array, yet no result pls help me out |
Re: Pls Help Me With This Php Code by yorex2011: 8:05pm On Jan 21, 2016 |
lillylove2:Try this <?php $query = "SELECT * FROM `table` WHERE `User`='dami24'"; $result = mysqli_query ($link, $query) ; $row = mysqli_fetch_assoc($result) ; $row_cnt = $result->num_rows; $sum = 0; $cnt = 0; //try 1 if 0 doesnt work properly while ($cnt < $row_cnt) { $row = mysqli_fetch_assoc($result) ; $amount = $row['amount'] ; $sum = $sum + $amount; $cnt++; } ? > |
Re: Pls Help Me With This Php Code by lillylove2(f): 8:17pm On Jan 21, 2016 |
yorex2011:It's showing error: Unexpexted T Variable on this line: $row_cnt = $result->num_rows; ... and the ending curl bracket ( } ). And please how do i echo the result out, and also store it into a vairable Thanks |
Re: Pls Help Me With This Php Code by yorex2011: 8:25pm On Jan 21, 2016 |
lillylove2: Ensure your semicolons are correct... Send a screen shot of the code if possible And mysqli is properly spelt |
Re: Pls Help Me With This Php Code by lillylove2(f): 8:36pm On Jan 21, 2016 |
yorex2011: Ive cross checked it. It's properly spelled. And no ommission. Pls help me out |
Re: Pls Help Me With This Php Code by lillylove2(f): 8:42pm On Jan 21, 2016 |
Pls help me out o |
Re: Pls Help Me With This Php Code by maekhel(m): 8:53pm On Jan 21, 2016 |
lillylove2:Start by showing us the codes you written that is not working. |
Re: Pls Help Me With This Php Code by lillylove2(f): 9:26pm On Jan 21, 2016 |
I cant believe all the programmers here are only programmers by mouth. It is well o |
Re: Pls Help Me With This Php Code by codemarshal08(m): 9:30pm On Jan 21, 2016 |
lillylove2: Please Show the code you tried. |
Re: Pls Help Me With This Php Code by yorex2011: 9:30pm On Jan 21, 2016 |
lillylove2:Be mindful of how you seek assistance.... 2 Likes |
Re: Pls Help Me With This Php Code by maekhel(m): 9:44pm On Jan 21, 2016 |
lillylove2:Only in nairaland programming section people will seek coding help without posting codes or screenshot of codes. Well not surprised, dhtml don kuku talk am before. 2 Likes |
Re: Pls Help Me With This Php Code by coldspot: 10:08pm On Jan 21, 2016 |
lol.. you can sum it up right from your SQL statement SELECT SUM(column_name) FROM table_name WHERE condition; Select sum(Amount) From Table Where user=dami24; 1 Like |
Re: Pls Help Me With This Php Code by lillylove2(f): 10:18pm On Jan 21, 2016 |
coldspot: Thanks. How do i echo the result? |
Re: Pls Help Me With This Php Code by sinequanon: 10:27pm On Jan 21, 2016 |
maekhel: Easier to just call yourself lillylove(f) and flutter your eyelashes. ![]() ![]() |
Re: Pls Help Me With This Php Code by losprince(m): 10:31pm On Jan 21, 2016 |
lillylove2: select sum(amount) as total from table where user = dami24 echo $row['total']; show your own code |
Re: Pls Help Me With This Php Code by hollyfat(m): 10:34pm On Jan 21, 2016 |
lillylove2: $sql = mysqli_query("SELECT SUM(Amount) as total FROM table where user='dami24'" ![]() $rs = mysqli_fetch_assoc($sql); $sum = $rs['total']; echo $sum; |
Re: Pls Help Me With This Php Code by lillylove2(f): 10:44pm On Jan 21, 2016 |
Thanks everybody for taking your time out to help me. Its finally working. |
Re: Pls Help Me With This Php Code by lillylove2(f): 10:46pm On Jan 21, 2016 |
hollyfat: Does this also work on regular mysql (not mysqli) ? I tried it, it didnt work |
Re: Pls Help Me With This Php Code by hollyfat(m): 11:04pm On Jan 21, 2016 |
lillylove2:. You can use mysql if mysqli didn't work. |
Re: Pls Help Me With This Php Code by lillylove2(f): 11:17pm On Jan 21, 2016 |
My God! Its multiplication im looking for not addition. Im totally unserious! Please help me out |
Re: Pls Help Me With This Php Code by hollyfat(m): 11:31pm On Jan 21, 2016 |
lillylove2:$sql = mysql_query("select amount from table where user='dami24'" ![]() $product = 1; //initialize your product while($rs = mysql_fetch_assoc($sql)){ $amt = $rs['amount']; //get the amount $product *= $amt; } echo $product; |
Re: Pls Help Me With This Php Code by lillylove2(f): 11:53pm On Jan 21, 2016 |
hollyfat: My God It Worked! Im really, really grateful. I can't thank you enough. I spent up to 3 hours before i was finally able to make the other one work, only to realize it was multiplication i was seeking, not addition. I had even given up, and accepted my fate that i will never be a programmer, only for me to try out ur code and it worked! Thanks very much. |
Re: Pls Help Me With This Php Code by hollyfat(m): 8:40am On Jan 22, 2016 |
lillylove2:You are welcome |
Re: Pls Help Me With This Php Code by A7(m): 9:35pm On Jan 22, 2016 |
lillylove2: This is how to make array_sum work. Set a variable holding an empty array: $loop_results = array(); Query your db: $sql = ("SELECT `amount` FROM `table` WHERE `user`='".dami24."'" ) ; $query = mysqli_query($con, $sql); If(mysqli_num_rows($query) > 0{ while($rows = mysqli_fetch_assoc($query){ $loop_results[] = $rows['amount']; } } If(empty($loop_results) == false){ $amount_sum = array_sum($loop_results); echo $amount_sum; } |
Re: Pls Help Me With This Php Code by A7(m): 9:49pm On Jan 22, 2016 |
lillylove2: In that case replace array_sum with array_product. |
(1) (Reply)
A Reminder That Abeg Was Built With PHP. / I Need Help On How To Upload Image To A Windows Based Application / Need Dope Ideas To Create A Social Network
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 29 |