Skip to main content


Showing posts from April, 2022

Get total of MySQL column and show highest totals

To get the total of a MySQL field and then select the three highest totals from the table, you can use the following PHP script: <?php // Assuming you have already established a MySQL connection // Retrieve the total of a field and select the three highest totals $servername = "localhost" ; $username = "your_username" ; $password = "your_password" ; $dbname = "your_database_name" ; // Create a new MySQL connection $conn = new mysqli( $servername , $username , $password , $dbname ); // Check the connection if ( $conn -> connect_error ) { die ( "Connection failed: " . $conn -> connect_error ); } // Prepare and execute the SQL statement to get the total and select three highest totals $sql = "SELECT SUM(field_name) AS total FROM table_name GROUP BY field_name ORDER BY total DESC LIMIT 3" ; $result = $conn -> query ( $sql ); if ( $result && $result -> num_rows > 0 )

Import CSV data into MySQL using PHP

Here's a detailed example of how to import data from a CSV file into a MySQL database using PHP. The script processes each row one by one, displays a success message for each successfully inserted row, and stops the process if any error occurs, showing the error message. Prerequisites:   Ensure you have a MySQL database and table set up to store the CSV data. Adjust the database connection details and table schema as needed. Database Setup: Assume you have a MySQL table named csv_import with columns id, name, and email. CREATE TABLE csv_import ( id INT AUTO_INCREMENT PRIMARY KEY , name VARCHAR ( 255 ) NOT NULL , email VARCHAR ( 255 ) NOT NULL ); Here's a PHP script that handles the CSV import process: <?php // Database connection details $servername = "your_servername" ; $username = "your_username" ; $password = "your_password" ; $dbname = "your_dbname" ; // Create connection $conn = new mysqli