我正在使用PHP PDO通過JSON將對象檢索到iOS應用程序。 有一個包含事件對象的數據庫表。它有三個字段來存儲日,月和年。數據庫的所有者不想將日期存儲在日期字段中,這意味着我必須處理它。PHP PDO語句篩選記錄
該應用程序發送一個網址使用參數,就像這樣:
http://..hidden domain./myfile.php?dia_inicio=16&dia_final=15&mes_inicio=11&mes_final=12&ano_inicio=2014&ano_final=2015
這意味着,我要尋找從16Nov2014到15Dec2015事件。
這是我的PHP代碼:
$sql = 'SELECT * FROM tbagenda WHERE (dia_evento => :di AND mes_evento = :mi AND ano_evento = :ai) OR (dia_evento <= :df AND mes_evento = :mf AND ano_evento = :af) ';
// use prepared statements, even if not strictly required is good practice
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':di', $dia_inicio, PDO::PARAM_INT);
$stmt->bindParam(':df', $dia_final, PDO::PARAM_INT);
$stmt->bindParam(':mi', $mes_inicio, PDO::PARAM_INT);
$stmt->bindParam(':mf', $mes_final, PDO::PARAM_INT);
$stmt->bindParam(':ai', $ano_inicio, PDO::PARAM_INT);
$stmt->bindParam(':af', $ano_final, PDO::PARAM_INT);
// execute the query
$stmt->execute();
// fetch the results into an array
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
// convert to json
$json = json_encode($result);
// echo the json string
echo $json
我應該如何改變我的說法,使其與日期一樣工作:16Nov2014到28Nov2014(同月),16Nov2014到5Dec2014(不同的月份,同年)和16Nov2014到2015年5月02日(不同年份)?
後來補充:
iOS的登錄顯示的URL發送到PHP文件:
recuperar_eventos_dia.php?dia_inicio=11&dia_final=26&mes_inicio=11&mes_final=11&ano_inicio=2014&ano_final=2014
PHP部分:
$start = "$ano_inicio-$mes_inicio-$dia_inicio";
$end = "$ano_final-$mes_final-$dia_final";
// In the SQL, concatenate the columns to make a YYYY-MM-DD date string
// and cast it to a MySQL DATE type.
// That makes it possible to use BETWEEN
$sql = 'SELECT
*
FROM tbagenda
WHERE STR_TO_DATE(CONCAT_WS('-', ano_evento, mes_evento, dia_evento), "%Y-%m-%d")
BETWEEN :start AND :end';
// Bind and execute the statement with 2 parameters:
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':start', $start, PDO::PARAM_STR);
$stmt->bindParam(':end', $end, PDO::PARAM_STR);
$stmt->execute();
// fetch, etc...
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
// convert to json
$json = json_encode($result);
// echo the json string
echo $json;
而且現在從表tbagenda截圖:
_The所有者不希望存儲日期在日期field_ - 爲什麼不能過?至少要添加一個適當的日期字段,除了使這類問題容易處理外。 – 2014-11-22 03:07:51
@MikeW,感謝您的評論,但它沒有幫助。 – novato 2014-11-22 03:13:58