0

我想返回与用户输入的价格匹配的行,即从 $ 到 $。

<form action="" method="post">
   <input class="price" name="p1" type="text"> 
   to 
   <input class="price" name="p2" type="text"><br>
   <input type="submit" name="sprice" value="go" >
</form>

这是选择查询。它不会根据价格范围返回行。

if (isset($_POST["sprice"]) && (!empty($_POST["p1"])) && (!empty($_POST["p2"]))){
    $p1 = $_POST["p1"];
    $p2 = $_POST["p2"];

    $paginate = new pagination($page
        , 'SELECT * FROM test where price BETWEEN "$p1" AND "$p2" ORDER BY id desc'
        , $options
    );

 }
4

4 回答 4

1
'SELECT * FROM test where price BETWEEN "$p1" AND "$p2" ORDER BY id desc'

上面的表格不替换变量,你需要这个

"SELECT * FROM test where price BETWEEN '$p1' AND '$p2' ORDER BY id desc"

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.single

于 2013-08-09T17:04:53.770 回答
0

where price >= '$p1' AND price <= '$p2'

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html

于 2013-08-09T17:14:03.380 回答
0

使用以下代码

         SELECT * FROM test where price BETWEEN '$p1' AND '$p2' ORDER BY id desc"
          SELECT * FROM test where price >=$p1 AND price<=$p2 ORDER BY id desc"
于 2013-08-09T18:55:25.060 回答
0

您可以使用AND和比较运算符来指定范围。

"SELECT * FROM test where price where price >= '$p1' AND price <= '$p2' ORDER BY id desc"

只是为了确保(检查):试试下面的代码。

<?php

$host = "localhost"; // Host name 
$username = "----"; // Mysql username 
$password = "----"; // Mysql password 
$db_name = "------"; // Database name 

$conn = mysql_connect($host, $username, $password);
mysql_select_db($db_name, $conn) or die("cannot select DB");

$p1 = VALUE_To_CHECK_WITH_1; //$_POST["p1"];
$p2 = VALUE_To_CHECK_WITH_2; //$_POST["p2"];

$query = "SELECT * FROM test where price where price >= '$p1' AND price <= '$p2' ORDER BY id DESC";

//or you may use
// "SELECT * FROM test where price BETWEEN '$p1' AND '$p2' ORDER BY id desc"


$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
    $data[] = $row;
}

print_r($data);

?>
于 2013-08-09T19:41:56.453 回答