欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP+Mysql實(shí)現(xiàn)多關(guān)鍵字與多字段生成SQL語(yǔ)句的函數(shù)

 更新時(shí)間:2014年11月05日 09:17:06   投稿:shichen2014  
這篇文章主要介紹了PHP+Mysql實(shí)現(xiàn)多關(guān)鍵字與多字段生成SQL語(yǔ)句的函數(shù),涉及字符串與數(shù)組的操作,是構(gòu)造SQL語(yǔ)句非常實(shí)用的技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP+Mysql實(shí)現(xiàn)多關(guān)鍵字與多字段生成SQL語(yǔ)句的函數(shù)的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

先看實(shí)例:

復(fù)制代碼 代碼如下:
$keyword="1 2 3";
echo $sql=search($keyword,"enter_gongyin_pic","a+b+c"); //函數(shù)生成,沒(méi)有LIMIT,沒(méi)有ORDER BY

生成:
復(fù)制代碼 代碼如下:
SELECT * FROM `enter_gongyin_pic` WHERE `a` LIKE '%1%' OR `a` LIKE '%2%' OR `a` LIKE '%3%' OR `b` LIKE '%1%' OR `b` LIKE '%2%' OR `b` LIKE '%3%' OR `c` LIKE '%1%' OR `c` LIKE '%2%' OR `c` LIKE '%3%'

$keyword由POST或者GET獲得.按空格分開(kāi) 可以多字段去查找.

實(shí)現(xiàn)函數(shù)如下:

復(fù)制代碼 代碼如下:
function search($keyword,$table,$field) 

//======================================================== 
 
//形參說(shuō)明: 
//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(); 
   //把字符串去掉沒(méi)有用的空格叔祖元素 
   $i=0; 
   foreach ($newstring as $key => $value) { 
   if($value!="") 
   { 
   $newstring2[$i]=$value; 
   $i++; 
   } 
   } 
//把字符串去掉沒(méi)有用的空格叔祖元素, 
         
$result_count=count($newstring2); //得到的結(jié)果數(shù)量 
 
//下面生成SQL語(yǔ)句 
 
 
//********************** 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表)開(kāi)始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ì)有所幫助。

相關(guān)文章

最新評(píng)論