我想返回与用户输入相匹配的行价格,即从$到$。
<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
);
}
答案 0 :(得分: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
答案 1 :(得分:0)
where price >= '$p1' AND price <= '$p2'
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html
答案 2 :(得分: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"
答案 3 :(得分: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);
?>