We have seen SQL SELECT order to get information from MySQLi table. We can likewise utilize a restrictive proviso called WHERE statement to choose required records.
A WHERE provision with approaches sign (=) works fine where we need to do a definite match. Like if "name = 'sai'". Be that as it may, there might be a prerequisite where we need to sift through all the outcomes where name ought to contain "johar". This can be taken care of utilizing SQL LIKE provision alongside WHERE statement.
On the off chance that SQL LIKE provision is utilized alongside % characters, at that point it will work like a meta character (*) in UNIX while rattling off all the documents or indexes at order brief.
Without a % character, LIKE proviso is very much like equivalents sign alongside WHERE statement.
Syntax
Here is nonexclusive SQL punctuation of SELECT order alongside LIKE provision to bring information from MySQLi table −
SELECT field1, field2,...fieldN table_name1, table_name2...
WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'
- You can indicate any condition utilizing WHERE proviso.
- You can utilize LIKE statement alongside WHERE condition.
- You can utilize LIKE statement instead of equivalents sign.
- At the point when LIKE is utilized alongside % sign then it will work like a meta character search.
- You can indicate more than one conditions utilizing AND OR administrators.
- A WHERE...LIKE statement can be utilized alongside DELETE or UPDATE SQL order likewise to indicate a condition.
Using LIKE clause at Command Prompt
This will utilize SQL SELECT order with WHERE...LIKE proviso to bring chosen information from MySQLi table tutorials_inf.
Example
Following model will restore all the records from tutorials_inf table for which creator name closes with johar −
root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> SELECT * from tutorials_inf
-> WHERE name LIKE '%johar';
+----+-------+
| id | name |
+----+-------+
| 2 | johar |
+----+-------+
1 row in set (0.00 sec)
mysql>
Using LIKE clause inside PHP Script
You can utilize comparable punctuation of WHERE...LIKE statement into PHP work mysqli_query(). This capacity is utilized to execute SQL order and later another PHP work mysqli_fetch_array() can be utilized to bring all the chose information if WHERE...LIKE proviso is utilized alongside SELECT order.
However, in the event that WHERE...LIKE proviso is being utilized with DELETE or UPDATE order, at that point no further PHP work call is required.
Example
Evaluate the accompanying guide to restore all the records from tutorials_inf table for which name contains johar −
<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$dbname = 'TUTORIALS';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br>';
$sql = 'SELECT * FROM tutorials_inf WHERE name LIKE "%johar%"';
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s \n",$row["name"]);
mysqli_free_result($result);
mysqli_close($conn);
?>
The example yield ought to be this way −
Connected successfully
johar