简体   繁体   中英

To get value from 2 column in single row in php

could somebody help me, as I just very new in PHP

Description :-

  • In my table, I have 3 column: Adult, Child, Totalpax
  • I just give the value into Adult & Child column and leave Totalpax column empty
  • Then, I try to make a calculation so that Totalpax filed get the amount from Adult + Child column

What have I do :-

  • Below is my code..
  • The problem is - this code give me the total number from my table.
  • BUT - I just want to get the value just from singe row..
  • Is my code below wrong? if so, how to create a code, so that I just get the amount from single row?..

Thank in advance..

My Code :-

<?php 
foreach($pdo->query("SELECT SUM(adult+child) FROM test WHERE testID = testID") as $totalpax) {
echo $totalpax['SUM(adult+cwhild)'];}
?>

SUM is an aggregate function which totals all the rows for the given expression.

To get a value for each row separately simply add them:

SELECT adult+child as total FROM test

And then

echo $totalpax['total']

This is your query:

SELECT SUM(adult+child)
FROM test
WHERE testID = testID

Start with the WHERE clause. It is comparing a problem to itself -- so this returns information from all rows where testID is not NULL .

What information does it return? That information is the SUM() of two columns. This is an aggregation query that returns one row, which has one column which is the sum of adult + child on basically all rows in the table.

Then, I try to make a calculation so that Totalpax filed get the amount from Adult + Child column.

As you have described the problem, you want an update not a select . That would be:

update test
    set Totalpax = adult + child;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM