2016-10-10 114 views
-1

我對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; 
} 
} 
+1

什麼是'mysql_escape_mimic',如果你正在使用一個準備好的參數化查詢語句,爲什麼你會這樣做呢 – RiggsFolly

+0

你有4個參數,只有2個值在數組中! – RiggsFolly

+0

'「SELECT * FROM schedule_slot WHERE(startDate BETWEEN:f1 AND:u1)OR(endDate BETWEEN:f2 AND:u2);」;'和chnage相應的數組以保存4個參數 – RiggsFolly

回答

2

一是當事情進展不對您<?php標記爲之後添加這兩條線很多人在LIVE服務器上開發,在這些服務器上錯誤報告當然會被關閉,並且假設他們的代碼沒有問題,但實際上它會產生很多錯誤。

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 

然後設置PDO如果發生任何錯誤,你會看到他們在頁面上拋出異常

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

$newSlot = array(
        ":f1" => $startDate->format('Y-m-d H:i:s')), 
        ":u1" => $endDate->format('Y-m-d H:i:s')), 
        ":f2" => $startDate->format('Y-m-d H:i:s')), 
        ":u2" => $endDate->format('Y-m-d H:i:s')) 
       ); 


$query = "SELECT * FROM schedule_slot 
      WHERE (startDate BETWEEN :f1 AND :u1) 
       OR (endDate BETWEEN :f2 AND :u2)"; 

$result = $db->prepare($query); 
// now execute the prepared query on $result not $db 
$result = $result->execute($newSlot); 

$row = $result->fetch_object(); 

print "slot: " . $row->startDate; 

現在,否則你會看到一個異常拋出,但沒有抓到,這樣也將顯示在網頁

+0

謝謝你,這個伎倆。我無法使'db-> setAttribute ...'部分工作,但我編輯我的數組看起來像你的修復它。 – PrintlnParams