本文介紹了PDO 和 MySQL '之間'的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在嘗試讓 PDO 與 MySQL 的之間"一起工作.下面是我的代碼:
I'm trying to get PDO to work with a MySQL 'between'. Below is my code:
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$start_date = date('Y-m-d H:i:s', mktime(0, 0, 0, 11, 1, 2009));
$end_date = date('Y-m-d H:i:s', mktime(23, 59, 59, 11, 30, 2009));
$STH = $DBH->prepare("SELECT * FROM `table` WHERE `start_date` BETWEEN ':start_date' AND ':end_date'");
$STH->bindParam(':start_date', $start_date, PDO::PARAM_STR);
$STH->bindParam(':end_date', $end_date, PDO::PARAM_STR);
$STH->execute();
var_dump($row);
返回的是一個(gè)帶有0"或NULL"值的數(shù)組.當(dāng)我硬編碼結(jié)束日期時(shí),它就像 start_date
設(shè)置為 -1,重新調(diào)整 end_date
之前的所有行.那么,我在這里做錯(cuò)了什么?
What gets returned is an array with '0' or 'NULL' for values. When I hard code the end date, it acts as if start_date
is set to -1, retuning me all rows before the end_date
. So, what am I doing wrong here?
推薦答案
不要用單引號(hào)將值括起來.
Don't wrap the values with single quote.
$STH = $DBH->prepare("SELECT * FROM `table` WHERE `start_date` BETWEEN :start_date AND :end_date");
這篇關(guān)于PDO 和 MySQL '之間'的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!