我正在尝试使用PHP和SQL制作价格滑块,但是当我在此代码中遇到问题时我遇到了问题
$query = mysql_query("SELECT * FROM price WHERE phone_price BETWEEN" .$from. "AND" .$to. );
while($row = mysql_fetch_array($query)){
print $row['phone_name'];
print $row['phone_price'];
print '';
}
我想运行像SELECT * FROM price WHERE phone_price BETWEEN 300 AND 500
我正在制作测试版,因此我接受来自<input>
的$ from和$ to值,我想我在mysql_query中插入变量时出错了。
错误 - Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login\slide\slide.php on line 28
答案 0 :(得分:4)
您的查询中有错误。在BETWEEN和AND之后需要空格。否则php会像...BETWEEN123AND1234...
那样读取您的查询。你应该更好地使用引号来放置变量:
$query = mysql_query("SELECT * FROM `price` WHERE `phone_price` BETWEEN '".$from."' AND '".$to."'");
答案 1 :(得分:1)
$dsn = "mysql:host=localhost;dbname=my_database";
$pdo = new PDO($dsn, $login, $password);
$sh = $pdo->prepare("SELECT * FROM price WHERE phone_price BETWEEN :from AND :to");
$sh->bindParam(':from',$from,PDO::PARAM_INT);
$sh->bindParam(':to',$to,PDO::PARAM_INT);
$res = $sh->fetch_all(PDO::FETCH_ASSOC);