In the same SQL table, PHP queries the sum of two columns of data and then outputs it.

WBOY
Release: 2016-08-08 09:24:18
Original
1287 people have browsed it

//Calculate the sum of the order quantity

<code><span>$query_num</span> = <span>"SELECT SUM(order_num) from hcf_order_db where order_id = XXX"</span>;
<span>$arr_qn</span> = mysqli_query(<span>$dbc</span>,<span>$query_num</span>);
<span>list</span>(<span>$sum</span>)=mysqli_fetch_row(<span>$arr_qn</span>);</code>
Copy after login

//Calculate the sum of the order amount

<code><span>$query_amount</span> = <span>"SELECT SUM(order_amount) from hcf_order_db where order_id = XXX"</span>;
<span>$arr_qm</span> = mysqli_query(<span>$dbc</span>,<span>$query_amount</span>);
<span>list</span>(<span>$sum2</span>)=mysqli_fetch_row(<span>$arr_qm</span>);</code>
Copy after login

Then the PHP output:

<code><span><<span>td</span><span>class</span>=<span>'font-songti'</span><span>align</span>=<span>'center'</span>></span><span><span><?php</span><span>echo</span><span>$sum</span><span>?></span></span><span></<span>td</span>></span><span><<span>td</span><span>class</span>=<span>'font-cali'</span><span>align</span>=<span>'right'</span>></span><span><span><?php</span><span>echo</span><span>$sum2</span><span>?></span></span><span></<span>td</span>></span></code>
Copy after login

If the output number is followed by many decimal places, you can set the attribute of the value in SQL Set to DECIMAL(16,1).
The following 1 means retaining 1 decimal point.

The above introduces the same table in SQL, and PHP queries the sum of two columns of data and outputs it, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template