我對php和pdos非常陌生。我的一位朋友基本上創建了一個pdo類和一些如何使用它的例子,這對我來說非常有用。但是現在我想要使用BETWEEN
mysql關鍵字執行查詢,並返回與條件匹配的任何內容,但它只是空白。我創建了mysql_query.log
文件,並從我可以從中收集的查詢準備好但未執行。我會告訴你從日誌中第二我的發現,讓我趕緊只是告訴你我的代碼:php pdo準備查詢但不執行它
$newSlot = array(
"fromDate" => $db->mysql_escape_mimic($startDate->format('Y-m-d H:i:s')),
"untilDate" => $db->mysql_escape_mimic($endDate->format('Y-m-d H:i:s'))
);
$query = "SELECT * FROM schedule_slot WHERE (startDate BETWEEN :fromDate AND :untilDate) OR (endDate BETWEEN :fromDate AND :untilDate);";
$result = $db->prepare($query);
$slot = null;
if($result == 1) {
$result = $db->execute($newSlot);
if($result == 1) {
$slot = $db->fetch();
}
}
print "slot: " . $slot["startDate"];
這裏是日誌的適用部分(這是我整理了一下):
161010 20:59:31
2 Connect [email protected] as anonymous on test
2 Prepare SELECT * FROM schedule_slot WHERE (startDate BETWEEN ? AND ?) OR (endDate BETWEEN ? AND ?)
2 Close stmt
2 Quit
下面是從日誌查詢的一個例子,實際上制定了罰款對我來說:
161010 21:01:07
3 Connect [email protected] as anonymous on test
3 Prepare INSERT INTO schedule_slot(startDate, endDate) VALUES(?,?)
161010 21:01:08
3 Execute INSERT INTO schedule_slot(startDate, endDate) VALUES('2016-10-11 13:35:00','2016-10-11 14:35:00')
3 Close stmt
3 Quit
我們,如果你要我編輯的PDO代碼或其他任何東西的,但據我可以我知道告訴它是一個標準的pdo類。請讓我知道爲什麼我的查詢不返回任何東西
編輯:這裏的PDO類,文件名dbpdo.php
:
<?php
class dbpdo {
private $_connection;
private $_statement;
public function __construct() {}
public function connect($host, $username, $password, $database) {
$connect = 'mysql:host='.$host.';dbname='.$database.';charset=utf8mb4';
$this->_connection = new PDO($connect, $username, $password);
$this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
public function __destruct() {
if ($this->_connection)
$this->_connection = null;
}
public function query($query){
try {
return $this->_connection->query($query);
} catch(PDOException $e) {
return "Error: " . $e->getMessage();
}
}
public function fetch(){
try {
return $this->_statement->fetch();
} catch(PDOException $e) {
return "Error: " . $e->getMessage();
}
}
public function prepare($query) {
try {
$this->_statement = $this->_connection->prepare($query);
return 1;
} catch(PDOException $e) {
return "Error: " . $e->getMessage();
}
}
public function execute($array) {
try {
$this->_statement->execute($array);
return 1;
} catch(PDOException $e) {
return "Error: " . $e->getMessage();
}
}
public function mysql_escape_mimic($inp) {
if(is_array($inp))
return array_map(__METHOD__, $inp);
if(!empty($inp) && is_string($inp)) {
return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $inp);
}
return $inp;
}
}
什麼是'mysql_escape_mimic',如果你正在使用一個準備好的參數化查詢語句,爲什麼你會這樣做呢 – RiggsFolly
你有4個參數,只有2個值在數組中! – RiggsFolly
'「SELECT * FROM schedule_slot WHERE(startDate BETWEEN:f1 AND:u1)OR(endDate BETWEEN:f2 AND:u2);」;'和chnage相應的數組以保存4個參數 – RiggsFolly