PHP MySQL ROUND() Function - GeeksforGeeks

01 Apr, 2021
In this article, we are going to see how MySQL ROUND() function works in PHP. The MySQL ROUND() function is used to round a number to specified decimal places. If no specified decimal places is provided for round-off then it rounds off the number to the nearest integer.
Syntax :
ROUND(X, D)
SELECT ROUND(column1), ... ROUND(column n)
from TABLE;
Here, we are taking the student database to perform round() operation.
Requirements:
XAMPP Server
PHP is a server-side scripting language that communicates with MySQL to manage the databases. MySQL is an open-source relational database management system (RDBMS). MySQL is developed, distributed, and supported by Oracle Corporation.
Database Table Screenshot:
MySQL Query to Round-off parent_salary:
SELECT parent_salary, ROUND(parent_salary) FROM student;
Result:
Parent salary : 50000 — rounded to : 50000Parent salary : 25000.8 — rounded to : 25001Parent salary : 10001 — rounded to : 10001Parent salary : 50000 — rounded to : 50000Parent salary : 50000 — rounded to : 50000Parent salary : 89000.1 — rounded to : 89000
MySQL Query to Round-off Student Marks:
SELECT name, ROUND(percentage) FROM student;
Result:
Student name : ojaswi — percentage : 79Student name : sravan kumar — percentage : 98Student name : bobby — percentage : 80Student name : ojaswi — percentage : 79Student name : rohith — percentage : 89Student name : gnanesh — percentage : 69
Approach:
Start the XAMPP server.
Open phpMyAdmin database manager and create a database named test.
Create a table named student inside the test database.
Insert the student’s records into the table. To insert the data into the table, we can use SQL query or can insert data directly into the table.
Write PHP code to perform the MySQL round() function and get the round-off data.
Program:
PHP
<?php  // Store the servername in a variable$servername = "localhost";  // Store the username in a variable$username = "root";  //Store the password in  a variable$password = "";  // Store the database name in a variable$dbname = "test";  // Create connection by passing these // connection parameters$conn = new mysqli($servername,     $username, $password, $dbname);  echo "Parent Salary";echo "<br><br>";  // SQL Query to select the data from// database tablr$sql = "SELECT parent_salary,     ROUND(parent_salary) FROM student";  $result = $conn->query($sql);  // Display data on the web pagewhile($row = mysqli_fetch_array($result)){    echo " Parent salary : " .         $row['parent_salary'] .        "   =>   Round-off : " .         $row['ROUND(parent_salary)']        . "<br>";}  echo "<br>*****************************";echo "<br>Students Percentage";echo "<br><br>";  // SQL Query to extract data from database$sql = "SELECT name, ROUND(percentage) FROM student";$result = $conn->query($sql);  // Display data on the web pagewhile($row = mysqli_fetch_array($result)){    echo " Student name  : ". $row['name'] .    "   =>   Percentage : "     . $row['ROUND(percentage)'] . "<br>";}  // Close the connection$conn->close();  ?>
Output:
PHP-function
PHP-MySQL
PHP-Questions
PHP
PHP Programs
SQL
Web Technologies
SQL
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to fetch data from localserver database and display on HTML table using PHP ?
How to pass form variables from one page to other page in PHP ?
Create a drop-down list that options fetched from a MySQL database in PHP
How to create admin login page using PHP?
Different ways for passing data to view in Laravel
How to call PHP function on the click of a Button ?
How to fetch data from localserver database and display on HTML table using PHP ?
How to pass form variables from one page to other page in PHP ?
How to create admin login page using PHP?
How to Install php-curl in Ubuntu ?