Infolinks

Breaking

Adsense

Friday, January 22, 2016

PHP MYSQL: Search Multiple Columns Using Like

Data is essential in an organization. So storing it in a database helps an organization to easily store it so they can retrieve it in the near future for decision-making. All information systems have the following common functions, to add or insert new record, update existing record, view list of records in the database and delete records. If you are going to create one, all you have to do is to reuse your codes since they are all the same. They just differ in the name of table, column names and variable names.

I already provided a tutorial on how to insert records in the database. This time, I will teach you how to display records from database to a table. Before you start, first create your database and table like on the picture below.

In this article, I will share with you how to search for a record in the database. Displaying the record have similar code when you want to search for a record. The only difference is you need to specify your condition in searching using the WHERE clause. 

I had encountered searching multiple columns using a single input control, a textbox. That is searching from a last name and first name column using any of the two. So I have here the code to do that.

STUDENT Table

STUDENT

MySQL Code Search

SELECT * FROM `student` WHERE CONCAT(stud_first,stud_last) like "%ua%"

The query above will search for first name or last name with "ua" and will display below.

Search Output








Since you want to search for multiple columns, you need to use the CONCAT function to combine what columns you are going to search. If you want to search in just a single column, below is an example.

SELECT * FROM `student` WHERE stud_first like "%ua%"

This will return the same output above since ua is found only on the first name Juan.


PHP MYSQL: Search Multiple Columns Using Like

No comments:

Post a Comment

Adbox