php mysql query where description like any of the word in the given string?

Almost all website having a search field on the top it will search in the entire database and display related data in the user interface.so when user type a group of word almost all case user will not get accurate report,because the MySQL query using the LIKE “%$query%/”,so in this tutorial we are planning to elaborate how to search word by word in the database for given string.


#search Form

<form action="search.php" method="post">
<input type="text" name="q" value="">

</form>

#search.php
$q=$_POST['q'];

$searchQ=explode(' ',$q );
$searchquery="";
foreach($searchQ as $key=>$value)
{
if($searchquery!='')
{
$searchquery.="description LIKE %".$value."%"
}
else
{
$searchquery.=" OR description LIKE %".$value."%"
}
}
$query="SELECT * FROM leads WHERE ".$searchquery;
you can use this query to fetch the data

Mysql query to fetch current month data

Sometimes we need to show only current month data in the application ,so for easy handling the data we should use a Mysql query to fetch current month data.Let we check out how to do that using one MySQL query

See below the Mysql query to fetch current month data


SELECT *
FROM leads
WHERE MONTH(date) = MONTH(CURRENT_DATE())
AND YEAR(date) = YEAR(CURRENT_DATE())
// leads= Table Name
// date = date field name


Here we are fetching current month data using the feature MONTH(CURRENT_DATE()) and MONTH(date)

MONTH(CURRENT_DATE()) will result 8 (since current month is Aug)

Next feature we are using in Mysql query to fetch current month data is YEAR(date) and YEAR(CURRENT_DATE())

YEAR(CURRENT_DATE()) will result 2019 (since today is Aug 24 2019)

so actual Mysql query to fetch current month data will work like this

SELECT *
FROM leads
WHERE MONTH(date) = 9
AND YEAR(date) = 2019

so it will fetch all result form database table leads that having date field value is in august month

how to insert Hindi language in MySQL,now ???? is coming in DB

We have to add different languages like Hindi,Marathi, Tamil etc inside text filed as per the required. Normally if you insert Hindi it will come ?????????? instead of characters in DB.To avoid this situation we have to set the DB to utf8 COLLATE utf8_unicode_ci.It support other character set also.Here am going to explain how to set it ,to insert Hindi language in MySQL we have to do below action

ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_unicode_ci;

How to insert Hindi in MySQL database using PHP, now am getting question mark

Run below SQL query to check default character set

SHOW VARIABLES LIKE ‘character\_set\_%’;

Variable_name             Value
character_set_client      utf8mb4
character_set_connection  utf8mb4
character_set_database    latin1
character_set_filesystem  binary
character_set_results     utf8mb4
character_set_server      latin1
character_set_system      utf8

here the character_set_database value is latin2 To insert Hindi in MySQL database using PHP, we need to update character_set_database value to utf8 ,ALTER DATABASE and change the DB character and set it to utf8 COLLATE utf8_bin

ALTER DATABASE DB_NAME CHARACTER SET utf8 COLLATE utf8_bin