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

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

Aug 08, 2016 am 09:24 AM
mysqli order query sum

//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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to sum columns in Excel How to sum columns in Excel May 16, 2023 pm 03:26 PM

How to Quickly View the Total of a Column in Excel If you just want to know the total of a column without adding that information to your spreadsheet, you can use the Excel status bar to quickly view the total of a column or any range of cells. To see the sum of a column using the Excel status bar: Highlight the data you want to summarize. To select an entire column, click the column name. Otherwise, drag cells to select your range. At the bottom of the screen, you'll see information about your selection, such as the average of all values ​​and the number of data points in the range. You will also see the sum of all values ​​in the selected cells. How to sum columns in Excel using AutoSum If you wish to add the sum of a column to a spreadsheet, there are many situations

Solution to PHP Fatal error: Call to undefined function mysqli_connect() Solution to PHP Fatal error: Call to undefined function mysqli_connect() Jun 23, 2023 am 09:40 AM

When writing web applications using PHP, a MySQL database is often used to store data. PHP provides a way to interact with the MySQL database called MySQLi. However, sometimes when using MySQLi, you will encounter an error message, as shown below: PHPFatalerror:Calltoundefinedfunctionmysqli_connect() This error message means that PHP cannot find my

PHP PDO vs. mysqli: compare and contrast PHP PDO vs. mysqli: compare and contrast Feb 19, 2024 pm 12:24 PM

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

What should I do if php cannot connect to mysqli? What should I do if php cannot connect to mysqli? Nov 09, 2022 am 10:07 AM

Solution to php unable to connect to mysqli: 1. Open the "php.ini" file; 2. Find "mysqli.reconnect"; 3. Change "mysqli.reconnect = OFF" to "mysqli.reconnect = on".

PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused Jun 23, 2023 am 08:54 AM

If you encounter the following error message when using PHP to connect to a MySQL database: PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused, then you can try to solve this problem by following the steps below. To confirm whether the MySQL service is running normally, you should first check whether the MySQL service is running normally. If the service is not running or fails to start, it may cause a connection refused error. you can

What is the running file of mysql What is the running file of mysql Apr 11, 2023 am 10:38 AM

The running file of mysql is mysqld; mysqld is an executable file, which represents the Mysql server program. Executing this file can directly start a server process; and mysqld_safe is a startup script, which will indirectly call mysqld and also start a monitor. process.

How to reorder multiple columns in Power Query via drag and drop How to reorder multiple columns in Power Query via drag and drop Mar 14, 2024 am 10:55 AM

In this article, we will show you how to reorder multiple columns in PowerQuery by dragging and dropping. Often, when importing data from various sources, columns may not be in the desired order. Reordering columns not only allows you to arrange them in a logical order that suits your analysis or reporting needs, it also improves the readability of your data and speeds up tasks such as filtering, sorting, and performing calculations. How to rearrange multiple columns in Excel? There are many ways to rearrange columns in Excel. You can simply select the column header and drag it to the desired location. However, this approach can become cumbersome when dealing with large tables with many columns. To rearrange columns more efficiently, you can use the enhanced query editor. Enhancing the query

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

See all articles