PHP+Mysql實(shí)現(xiàn)多關(guān)鍵字與多字段生成SQL語句的函數(shù)
本文實(shí)例講述了PHP+Mysql實(shí)現(xiàn)多關(guān)鍵字與多字段生成SQL語句的函數(shù)的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
先看實(shí)例:
echo $sql=search($keyword,"enter_gongyin_pic","a+b+c"); //函數(shù)生成,沒有LIMIT,沒有ORDER BY
生成:
$keyword由POST或者GET獲得.按空格分開 可以多字段去查找.
實(shí)現(xiàn)函數(shù)如下:
{
//========================================================
//形參說明:
//keyword為關(guān)鍵字,如“北京首都 方向 火車”。帶有空格或者不帶
//table為表名,如enter_gongyin_pic。
//field為字段組合,如查找一個(gè)字段就寫好 name
//如查找兩個(gè)以上就用 name+picdir
//========================================================
//首先確定field
$new_field=explode("+",$field); //按+剝離
$field_count=count($new_field); //得到的結(jié)果數(shù)量
$newstring=explode(" ",$keyword); //按空格剝離
$newstring2=array();
//把字符串去掉沒有用的空格叔祖元素
$i=0;
foreach ($newstring as $key => $value) {
if($value!="")
{
$newstring2[$i]=$value;
$i++;
}
}
//把字符串去掉沒有用的空格叔祖元素,
$result_count=count($newstring2); //得到的結(jié)果數(shù)量
//下面生成SQL語句
//********************** if($field_count==1) //找1個(gè)字段 START ****************************
if($field_count==1) //找1個(gè)字段
{
if($result_count==1) //判斷如果是一個(gè)關(guān)鍵段
{
$newstring_search=$newstring2[0];
$sql="SELECT *
FROM `$table`
WHERE `".$new_field[0]."` LIKE '%$newstring_search%'";
}
if($result_count>1) //判斷如果是多個(gè)關(guān)鍵段
{
$sql="SELECT *
FROM `$table`
WHERE ";
$sql_add="";
foreach ($newstring2 as $key => $value)
{
if($key==0)
{
$sql_add=$sql_add."`".$new_field[0]."` LIKE '%".$value."%'";
}
else
{
$sql_add=$sql_add." OR `".$new_field[0]."` LIKE '%".$value."%'";
}
}
$sql=$sql.$sql_add;
}
}
//********************** if($field_count==1) //找1個(gè)字段 END ****************************
//********************** if($field_count>1) //找多個(gè)字段 START ****************************
if($field_count>1) //找多個(gè)字段,這個(gè)時(shí)候$new_field是一個(gè)數(shù)組。擁有多個(gè)字段
{
if($result_count==1) //判斷如果是一個(gè)關(guān)鍵段
{
$newstring_search=$newstring2[0]; //$newstring_search是關(guān)鍵字
$sql="SELECT *
FROM `$table`
WHERE ";
$sql_add="";//新增加字段
foreach ($new_field as $key => $value)
{
if($key==0)
{
$sql_add=$sql_add."`".$value."` LIKE '%".$newstring_search."%'";
}
else
{
$sql_add=$sql_add." OR `".$value."` LIKE '%".$newstring_search."%'";
}
}
$sql=$sql.$sql_add;
}
if($result_count>1) //判斷如果是多個(gè)關(guān)鍵段(多個(gè)關(guān)鍵字)==========================
{
$sql="SELECT *
FROM `$table`
WHERE ";
$sql_add="";//新增加字段
foreach ($new_field as $key => $value)
{
if($key==0) //遇到$new_field[0]時(shí)候 例:`a` LIKE '%1%' OR `a` LIKE '%2%' OR `a` LIKE '%3%'
{ //嵌套foreach
foreach ($newstring2 as $key2 => $value2)
{
if($key2==0)
{
$sql_add=$sql_add."`".$value."` LIKE '%".$value2."%'";
}
else
{
$sql_add=$sql_add." OR `".$value."` LIKE '%".$value2."%'";
}
}
//嵌套foreach
}
else
//(如果是多字段的比如查name+picdir表)開始FOREACH連續(xù)循環(huán),每次執(zhí)行ELSE $new_field[1] $new_field[2] $new_field[3]。
//對(duì)應(yīng)的值為$value
{
//嵌套foreach(多字段與多關(guān)鍵字)
foreach ($newstring2 as $key2 => $value2)
{
if($key2==0)
{
$sql_add=$sql_add." OR `".$value."` LIKE '%".$value2."%'";
}
else
{
$sql_add=$sql_add." OR `".$value."` LIKE '%".$value2."%'";
}
}
//嵌套foreach
}
}//foreach ($new_field as $key => $value)結(jié)束
$sql=$sql.$sql_add;
}//if($result_count>1)結(jié)束
}//if($field_count>1) 結(jié)束
//********************** if($field_count>1) //找多個(gè)字段 END ****************************
return $sql;
}
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
- MySQL中Order By多字段排序規(guī)則代碼示例
- php+mysql+ajax實(shí)現(xiàn)單表多字段多關(guān)鍵詞查詢的方法
- MySQL中索引優(yōu)化distinct語句及distinct的多字段操作
- MySQL中按照多字段排序及問題解決
- MySQL注入繞開過濾的技巧總結(jié)
- MYSQL使用正則表達(dá)式過濾數(shù)據(jù)
- Python連接MySQL并使用fetchall()方法過濾特殊字符
- python實(shí)現(xiàn)mysql的單引號(hào)字符串過濾方法
- mysql 復(fù)制過濾重復(fù)如何解決
- Mysql數(shù)據(jù)庫實(shí)現(xiàn)多字段過濾的方法
相關(guān)文章
PHP json_decode函數(shù)詳細(xì)解析
本篇文章主要是對(duì)PHP json_decode函數(shù)進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-02-02
解決安裝WampServer時(shí)提示缺少msvcr110.dll文件的問題
下面小編就為大家?guī)硪黄鉀Q安裝WampServer時(shí)提示缺少msvcr110.dll文件的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
php設(shè)計(jì)模式 Command(命令模式)
將一個(gè)請(qǐng)求封裝為一個(gè)對(duì)象從而使你可用不同的請(qǐng)求對(duì)客戶進(jìn)行參數(shù)化,對(duì)請(qǐng)求排除或記錄請(qǐng)求日志,以及支持可取消的操作2011-06-06
php中file_get_contents()函數(shù)用法實(shí)例
在本文中我們給大家整理了關(guān)于php中file_get_contents()函數(shù)的相關(guān)用法,需要的朋友們學(xué)習(xí)參考下。2019-02-02
PHP使用pcntl_fork實(shí)現(xiàn)多進(jìn)程下載圖片的方法
這篇文章主要介紹了PHP使用pcntl_fork實(shí)現(xiàn)多進(jìn)程下載圖片的方法,較為詳細(xì)的分析了pcntl_fork的原理與用法,以及使用pcntl_fork實(shí)現(xiàn)多進(jìn)程下載圖片的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
php 來訪國內(nèi)外IP判斷代碼并實(shí)現(xiàn)頁面跳轉(zhuǎn)
最近做了公司網(wǎng)站的一個(gè)需求:按來訪人的IP來判斷他是進(jìn)中文網(wǎng)站還是英文網(wǎng)站。2009-12-12
php中讓人頭疼的浮點(diǎn)數(shù)運(yùn)算分析
這篇文章主要介紹了php中讓人頭疼的浮點(diǎn)數(shù)運(yùn)算,結(jié)合實(shí)例形式分析了php針對(duì)浮點(diǎn)數(shù)運(yùn)算round函數(shù)的使用方法,需要的朋友可以參考下2016-10-10

