3 回答

TA貢獻1862條經驗 獲得超7個贊
<?php
function strsToArray($strs) {
$result = array();
$array = array();
$strs = str_replace(',', ',', $strs);
$strs = str_replace("n", ',', $strs);
$strs = str_replace("rn", ',', $strs);
$strs = str_replace(' ', ',', $strs);
$array = explode(',', $strs);
foreach ($array as $key => $value) {
if ('' != ($value = trim($value))) {
$result[] = $value;
}
}
foreach($result as $k=>$v){
$sql="";
$sql="select * from table where 查詢字段 = '".$v."' ";
$row = mysql_query($sql);
if($ret = mysql_fetch_assoc($row)){
print_r($ret);
}else{
echo "沒有找到值為".$v."的數據";
}
echo "<br>";
}
}
$strs = $_GET["zi"];
strsToArray($strs);
?>

TA貢獻1842條經驗 獲得超13個贊
//示例代碼:index.php
<?php //請求URL示例:http://localhost/index.php?zi=111,zz,ddd //獲取參數 $strs = $_GET [ "zi" ]; //調用函數(strsToArray) 構造查詢sql條件 $where = strsToArray( $strs ); //連接數據庫 $con = mysql_connect( "localhost" , "root" , "root" ); if (! $con ) { die ( 'Could not connect: ' . mysql_error()); } mysql_select_db( "my_db" , $con ); //拼裝sql、結果如:SELECT * FROM test where 1=1 and title like '%111%' and title like '%zz%' and title like '%ddd%' $sql = "SELECT * FROM test where 1=1 " . $where ; echo $sql ; exit ; $result = mysql_query( $sql ); echo "查詢信息如下:" ; while ( $row = mysql_fetch_array( $result )) { echo $row [ '字段2' ] . "=====" . $row [ '字段三' ]; echo "<br />" ; } mysql_close( $con ); function strsToArray( $strs ) { $where = "" ; $array = array (); $strs = str_replace ( ',' , ',' , $strs ); $strs = str_replace ( "n" , ',' , $strs ); $strs = str_replace ( "rn" , ',' , $strs ); $strs = str_replace ( ' ' , ',' , $strs ); $array = explode ( ',' , $strs ); foreach ( $array as $key => $value ) { if ( '' != ( $value = trim( $value ))) { $where .= " and title like '%{$value}%' " ; } } return $where ; } ?> |

TA貢獻1808條經驗 獲得超4個贊
首先連接數據庫
$conn = mysql_connect( 'localhost' , 'root' , 'mypassword' ); //連接數據庫 mysql_select_db( 'mydatabase' ); //選擇庫 mysql_query( 'set names mycharset' ); //設置編碼 |
調用函數得到關鍵字數組
$arr = strsToArray( $strs ); //得到要查詢的關鍵字數組 |
遍歷查詢
$result = array (); //初始化結果數組 foreach ( $arr as $keyword ) //遍歷數組 { $sql = "select * from mytable where myfield like '%$keyword%'" ; //構造SQL語句 $obj = mysql_query( $sql ); //查詢數據庫 $result = array_merge ( $result , mysql_fetch_array( $obj )); //取得結果數組 } $result = array_unique ( $result ); //去重 |
- 3 回答
- 0 關注
- 118 瀏覽
添加回答
舉報