gpt4 book ai didi

php - 在mysql查询中使用多个WHERE条件

转载 作者:行者123 更新时间:2023-11-29 14:18:10 25 4
gpt4 key购买 nike

我正在尝试将多个 WHERE 条件应用于我的查询,如下所示:

$hotel="Hotel Name";
$data1=explode('/',$data1);
$newdata1=mktime(0, 0, 0, $data1[0], $data1[1], $data1[2]);
$newdata1=date("Y-m-d", $newdata1);

$data2=explode('/',$data2);
$newdata2=mktime(0, 0, 0, $data2[0], $data2[1], $data2[2]);
$newdata2=date("Y-m-d", $newdata2);

$filtro=mysql_query("SELECT *
FROM hotels_rates_flat
WHERE htl_name = $hotel AND
given_date>=$newdata1 AND
given_date<=$newdata2
ORDER BY htl_name, city_zone, given_date")
or die(mysql_error());

它返回以下错误:

*您的 SQL 语法有错误;检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“do Hotel AND Gived_date>=2012-09-01 AND Give_date<=2012-09-05 ORDER BY htl_nam”附近使用的正确语法。*

这真的让我很困扰,因为我看不出任何明显的错误。

如果有任何帮助,我将不胜感激。

谢谢

最佳答案

使用 PDO 避免 SQL 注入(inject)和错误报告:

<?php
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=UTF-8', 'username', 'password', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$hotel="Hotel Name";
$data1=explode('/',$data1);
$newdata1=mktime(0, 0, 0, $data1[0], $data1[1], $data1[2]);
$newdata1=date("Y-m-d", $newdata1);

$data2=explode('/',$data2);
$newdata2=mktime(0, 0, 0, $data2[0], $data2[1], $data2[2]);
$newdata2=date("Y-m-d", $newdata2);

try {
$stmt = $db->query("SELECT * FROM `hotels_rates_flat` WHERE `htl_name` = :hotel AND (`given_date` BETWEEN :date1 AND :date2) ORDER BY `htl_name`, `city_zone`, `given_date`");
$stmt->execute(array(':hotel' => $hotel,':date1' => $newdata1,':date2' => $newdata2));
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $ex) {
echo $ex->getMessage();
}
// use $results
?>

关于php - 在mysql查询中使用多个WHERE条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12223731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com