

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to write PHP script by using LIKE clause inside it to match the data from MySQL table?
We can use the similar syntax of the WHERE...LIKE clause into the PHP function – mysql_query(). This function is used to execute the SQL command and later another PHP function – mysql_fetch_array() can be used to fetch all the selected data if the WHERE...LIKE clause is used along with the SELECT command.
But if the WHERE...LIKE clause is being used with the DELETE or UPDATE command, then no further PHP function call is required.
To illustrate it we are having the following example −
Example
In this example, we are writing a PHP script that will return all the records from the table named ‘tutorial_tbl’ for which the author name contains ‘jay’ −
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT tutorial_id, tutorial_title, tutorial_author, submission_date FROM tutorials_tbl WHERE tutorial_author LIKE "%jay%"'; mysql_select_db('TUTORIALS'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo "Tutorial ID :{$row['tutorial_id']} <br> ". "Title: {$row['tutorial_title']} <br> ". "Author: {$row['tutorial_author']} <br> ". "Submission Date : {$row['submission_date']} <br> ". "--------------------------------<br>"; } echo "Fetched data successfully\n"; mysql_close($conn); ?>
- Related Questions & Answers
- How to write PHP script by using ORDER BY clause inside it to sort the data of MySQL table?
- How to write PHP script by using MySQL JOINS inside it to join two MySQL tables?
- How to write PHP script to delete data from an existing MySQL table?
- How to write PHP script to fetch data, based on some conditions, from MySQL table?
- How to write PHP script to update an existing MySQL table?
- How can we insert data into an existing MySQL table by using PHP script?
- How can we create a MySQL table by using PHP script?
- How Can MySQL GROUP BY clause behave like DISTINCT clause?
- How can we delete an existing MySQL table by using PHP script?
- How can we create a MySQL temporary table by using PHP script?
- How can we write PHP script to count the affected rows by MySQL query?
- Which PHP functions are used in the PHP script to fetch data from an existing MySQL table?
- How to create a new table from the first table using MySQL LIKE?
- Write an example to establish MySQL database connection using PHP script?
- How can we fetch all the data from MySQL table by using mysql_fetch_array() function, returning an array with the numeric index, in PHP script?
Advertisements