How to use Order by with Multiple columns in MySQL

The data is not displayed in any particular order when you simply fetch it from the MySQL table. For displaying data in some meaningful way ORDER BY clause is used.

This sorts your result on the basis of the column name specified in the clause within the SELECT query.

It allows us to –

  • Specify ordering in single or multiple columns
  • Define sort results in ASC or DESC format.

How to use Order by with Multiple columns in MySQL

Read more

Get checked Checkboxes value with PHP

Checkboxe­s are a versatile tool found on many we­bsites, allowing users to sele­ct their preferre­d choices among different options available­ with ease.

When you use it in your form and try to read all checked values as any other elements like –  text box, text area, radio button, etc.

echo $_POST['lang'];  // Checkbox element

you will get the last checked value.

You need to send the checkboxes value in the form of an Array when the form gets submitted then you can loop over $_POST values.

In this tutorial, I show how you can read submitted checked checkboxes values with PHP and also show how you can save it to the MySQL database.

Get checked Checkboxes value with PHP

Read more

How to Break the nested loop in PHP

The break statement in PHP is utilized to terminate the execution of the current loop or switch case statement. However, when dealing with nested loops and the intention is to exit from one or more of the outer loops, you can achieve this by providing a numeric value with the break statement.

This tutorial demonstrates the process of terminating a specified number of nested foreach, for, while, and do-while loops in PHP by utilizing the break statement.

How to Break the nested loop in PHP

Read more

Get Last insert id from MySQL Table with PHP

There are various approaches to selecting the last insert ID from the MySQL table:

  • Fetch the ID by selecting a row in descending order and storing the ID.
  • Retrieve the maximum value.
  • The query below provides the subsequent AUTO_INCREMENT value for the chosen table, useful for acquiring the last ID.
SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'user'

For example, if your AUTO_INCREMENT column’s last value is 8, executing the query will return 9.

In PHP there is an inbuilt method that simply returns the last insert id according to the previous insert query.

Get Last insert id from MySQL Table with PHP

Read more