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条经验 获得超12个赞
//示例代码: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 关注
- 90 浏览
添加回答
举报