需要帮助WHERE rlike和之间的搜索代码

时间:2013-02-16 00:08:44

标签: php mysql

你好,你们可以指出我的错误:(我想对我的页面进行多列搜索: 所以我的剧本就是这样:

<?
$search=$_GET['s'];

$query="SELECT * FROM `filmi` WHERE `nomer` rlike '$search' or `rezume` rlike '$search' or `kategoriq` rlike '$search' or ' order by seriq asc ";
?>

想按价格范围进行搜索,所以我创建了这个:

<?
$search=$_GET['s'];
$pricemin=$_GET['min'];
$pricemax=$_GET['max'];
$query="SELECT * FROM `filmi` WHERE `nomer` rlike '$search' or `rezume` rlike '$search' or `kategoriq` rlike '$search' or 'seriq' BETWEEN '$pricemin' and '$pricemax'  order by seriq asc ";
?>

如何在它之间取代这种情况?我看一些其他人的搜索引擎代码,他们使用(SELECT * FROM table WHERE Table1 LIKE '%$table1%' AND Table2 LIKE '%$table2%' AND Price BETWEEN '%$min%','%$max%'"); 但它与我的不起作用;(请帮忙

2 个答案:

答案 0 :(得分:0)

使用BETWEEN按此价格范围过滤结果集

$query="SELECT * FROM `filmi` 
         WHERE (`nomer` RLIKE '$search' OR 
                `rezume` RLIKE '$search' OR 
                `kategoriq` RLIKE '$search') AND 
                `seriq` BETWEEN '$pricemin' AND '$pricemax'  
         ORDER BY seriq";

这是sqlfiddle

正如@cryptic建议考虑使用mysqli_*PDO准备好的语句。

UPDATE3:这就是您在mysqli案例中使用预准备语句的方法,并且可以按关键字,关键字和价格范围或仅按价格范围进行搜索:

$stype = 0;
if (isset($_GET['s']) && $_GET['s']){
    $search=$_GET['s'];
    $stype += 1;
}
if (isset($_GET['min']) && $_GET['min'] && 
    isset($_GET['max']) && $_GET['max']){
    $pricemin=$_GET['min'];
    $pricemax=$_GET['max'];
    $stype +=2;
}
if (!$stype) {
    echo "Required parameter(s) missing.";
    exit;
}
/* connect to the database*/
$db = new mysqli("localhost", "user", "password", "dbname");
/* check connection */
if ($db->connect_errno) {
    echo "Connection failed: " . $db->connect_error;
    exit();
}
$query="SELECT * FROM `filmi` WHERE ";
if ($stype == 1) {
    $query .= " (`nomer` rlike ? OR `rezume` rlike ? OR `kategoriq` rlike ?) ";
} elseif ($stype == 2) {
    $query .= " `seriq` BETWEEN ? AND ? ";
} elseif ($stype == 3) {
    $query .= " (`nomer` rlike ? OR `rezume` rlike ? OR `kategoriq` rlike ?) AND `seriq` BETWEEN ? AND ?";
}
$query .= " ORDER BY seriq";
/* create a prepared statement */
if (!$stmt = $db->prepare($query)) {
    //handle error here;
    echo "Error preparing statement.";
    exit;
}
/* bind parameters for markers */
if ($stype == 1) {
    $stmt->bind_param("sss", $search, $search, $search);
} elseif ($stype == 2) {
    $stmt->bind_param("dd", $pricemin, $pricemax);
} elseif ($stype == 3) {
    $stmt->bind_param("sssdd", $search, $search, $search, $pricemin, $pricemax);
}
/* execute query */
$stmt->execute();
/* get result */
$result = $stmt->get_result();
if ($result) {
/* now you can fetch the results into an assoc array */
    while ($row = $result->fetch_assoc()) {
        echo $row['nomer']. ", " .$row['rezume']. ", " .$row['kategoriq']. ", " .$row['seriq']. "<br>";
    }
}
/* close statement */
$stmt->close();
/* close db connection */
$db->close();

答案 1 :(得分:0)

查询(忽略SQL注入):

SELECT * FROM table 
WHERE field1 LIKE '%$table1%' 
AND field2 LIKE '%$table2%' 
AND Price BETWEEN 'val3' AND 'val2'

您无法使用BETWEEN进行like比较。