简体   繁体   English

php pdo准备查询但不执行查询

[英]php pdo prepares query but does not execute it

I'm really new to php and pdos. 我真的是php和pdos的新手。 A friend of mine basically created a pdo class and a couple of examples of how to use it and that's been working great for me. 我的一个朋友基本上创建了一个pdo类和几个使用它的示例,这对我来说一直很有效。 But now I want to do a query that uses the BETWEEN mysql keyword and returns anything that matches the criteria but it just comes up blank. 但是,现在我想执行一个使用BETWEEN mysql关键字的查询,并返回符合条件的任何内容,但它只是空白。 I have created mysql_query.log file and from what I can gather from it the query gets prepared but not executed. 我已经创建了mysql_query.log文件,从中我可以从中收集到查询已准备但未执行。 I'll show you my findings from the log in a second, let me quickly just show you my code: 我将在一秒钟内向您展示我在日志中的发现,让我快速向您展示我的代码:

$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"];

Here's the applicable part of the log (which I tidied up a bit): 这是日志的适用部分(我整理了一下):

161010 20:59:31     
    2 Connect   root@localhost as anonymous on test
    2 Prepare   SELECT * FROM schedule_slot WHERE (startDate BETWEEN ? AND ?) OR (endDate BETWEEN ? AND ?)          
    2 Close stmt                
    2 Quit  

And here's an example from the log of a query that actually worked out fine for me: 这是来自查询日志的一个示例,对我来说实际上很合适:

161010 21:01:07     
    3 Connect   root@localhost 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  

Let me know if you want me to edit the pdo code or anything else in but as far as I can tell it's a standard pdo class. 让我知道您是否要编辑pdo代码或其他内容,但据我所知这是标准的pdo类。 Please let me know why my query isn't returning anything 请让我知道为什么我的查询未返回任何内容

Edit : Here's the pdo class, filename dbpdo.php : 编辑 :这是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;
}
}

First when things are going wrong add these 2 lines after your <?php tag as far to many people develop on LIVE servers where error reporting will of course be turned off, and assume there is nothing wrong with their code when in reality it is generating lots of errors. 首先,当出现问题时,请在您的<?php标记后添加这2行,以便尽可能多的人在LIVE服务器上进行开发,这些服务器当然会关闭错误报告,并假定在实际生成代码时它们的代码没有任何问题很多错误。

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

Then set PDO to throw exceptions 然后将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;

Now if any errors occur you will see them on the page, or you will see an exception throw, but not caught, so that will also show on the web page 现在,如果发生任何错误,您将在页面上看到它们,或者看到抛出异常但未被捕获的异常,因此这也将显示在网页上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM