简体   繁体   English

PHP-仅在执行一次execute()时执行两次PDO查询

[英]PHP - PDO Query executing twice when execute() is only called once

I have looked everywhere for an answer to this and tried everything I have found here on StackOverflow and other sites. 我到处都在寻找答案,并尝试了在StackOverflow和其他站点上找到的所有内容。

Basically what's happening is that whenever I execute an SQL query, PDO is executing it twice even when execute() is only called once... 基本上发生的是,每当我执行一个SQL查询时,即使execute()仅被调用一次,PDO也会执行两次。

Here is my code... 这是我的代码...

<?php
namespace quizazle;

class sql{
  private $username = 'x';
  private $passwd = '';
  private $port = 3306;
  private $host = 'x';
  private $name = 'x';
  private $charSet = 'utf8mb4';

  private $db = null;

  public function __construct(){
    $this->db = new \PDO("mysql:host=$this->host;dbname=$this-    >name;charset=$this->charSet", $this->username, $this->passwd);
    $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
    $this->db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
    $this->db->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
  }

  private function bind($q, $p){
    try{
      $s = $this->db->prepare($q);
      if(!empty($p)){
        foreach ($p as $pm){
          $s->bindParam($pm['key'], $pm['value']);
        }
      }
    }
    catch(PDOException $e){
      echo($e->getMessage());
    }
    return $s;
  }

  public function query($query, $params){
    try{
      $statement = $this->bind($query, $params);
      $statement->execute();
      $res = $statement->fetchAll(\PDO::FETCH_BOTH);

      return $res;
    }
    catch(PDOException $e){
      echo($e->getMessage());
    }
  } 

  public function update($query, $params){
    try{
      $statement = $this->bind($query, $params);
      $statement->execute();
      $res = $statement->rowCount();

      return $res;
    }
    catch(PDOException $e){
      echo($e->getMessage());
    }
  }
}
?>

and here is the code where I am using the sql class... 这是我使用sql类的代码...

$sql = new quizazle\sql();

$params = array(
  array(
    'key' => 'd',
    'value' => 'a'
  )
);

$result = $sql->update("INSERT INTO `test` (`data`) VALUES(:d)", $params);

var_dump($result);

I have tried... 我努力了...

  • removing var_dump($result); 删除var_dump($result);
  • not binding parameters, although that is a bad way to go for me, it still doesn't work. 不绑定参数,尽管这对我来说是一个不好的方法,但仍然无法正常工作。
  • changing $this->db->setAttribute(\\PDO::ATTR_EMULATE_PREPARES, true); 更改$this->db->setAttribute(\\PDO::ATTR_EMULATE_PREPARES, true); to $this->db->setAttribute(\\PDO::ATTR_EMULATE_PREPARES, false); $this->db->setAttribute(\\PDO::ATTR_EMULATE_PREPARES, false);
  • changing $this->db->setAttribute(\\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); 更改$this->db->setAttribute(\\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); to $this->db->setAttribute(\\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); $this->db->setAttribute(\\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

yet none of this has worked. 然而,这些都没有奏效。 Any help will be greatly appreciated and thank you all in advance :) 任何帮助将不胜感激,并提前谢谢大家:)

PS: I discovered this problem while running INSERT statements, if that helps at all. PS:如果有帮助,我在运行INSERT语句时发现了此问题。

  1. You wrote a very strange class that made your experience with database much worse than with vanilla PDO. 您编写了一个非常奇怪的类,这使您的数据库体验比使用普通PDO 糟糕得多。
  2. Most likely your implementation of SEO-friendly URLS is no better. 最有可能您实施的SEO友好URLS并没有更好。

To solve the first problem, leave PDO alone. 要解决第一个问题,请不要理会PDO。 Keep only one method in your class - query() , make it return PDOStatement : 在类中只保留一种方法query() ,使其返回PDOStatement

public function query($query, $params) {
  $statement = $this->db->prepare($query);
  $statement->execute($params);
  return $statement;
} 

and utilize the method chaining to get different kinds of results from it. 并利用方法链从中获得不同种类的结果。 You may read detailed explanations in my article Your first database wrapper's childhood diseases . 您可以在我的文章“ 您的第一个数据库包装程序的童年疾病”中阅读详细的解释。 So your application code will become much cleaner: 因此,您的应用程序代码将变得更加简洁:

$sql = new quizazle\sql();
$params = array('d' => 'a');
$result = $sql->query("INSERT INTO `test` (`data`) VALUES(:d)", $params);
var_dump($result->rowCount());

While for the second, just do not make your router script act as 404 handler . 对于第二个,只是不要让您的路由器脚本充当404处理程序 Run any controllers only if they are intended to, based on the request parameters. 根据请求参数,仅在需要时运行任何控制器。 Do not run any SQL for the every request you get, but do it only for the requests you expect. 不要为收到的每个请求运行任何SQL,而仅对期望的请求运行。

I'm sorry for wasting all of your time, this was my stupid mistake. 很抱歉浪费您所有的时间,这是我的愚蠢错误。

You see, I was using Blisk which is a browser designed for developers. 您看,我使用的是Blisk ,这是专为开发人员设计的浏览器。 Since it is designed for developers, it has a function built-in that allows you to view your website as if it were on a mobile device, like this... 由于它是为开发人员设计的,因此它具有内置功能,可让您像在移动设备上一样浏览网站,就像这样... 在此处输入图片说明

When the page is loaded in this browser, it is loaded twice, once for the PC view and once for the mobile view. 在此浏览器中加载页面后,页面将被加载两次,一次用于PC视图,一次用于移动视图。 This means any PHP functions will be called twice, since the page is effectively being loaded twice, and that was the root of my problem. 这意味着任何PHP函数都会被调用两次,因为该页面实际上已被加载了两次,这就是我问题的根源。

There was nothing wrong with the code or the server, it was just me using Blisk that was the problem. 代码或服务器没有问题,只有我使用Blisk才是问题所在。 Now that I'm back on Chrome, it's all alright! 现在,我回到了Chrome,一切正常!

I have flagged the problem to the Blisk devs, so hopefully something can be done to fix this! 我已将问题标记给Blisk开发人员,因此希望可以做一些事情来解决此问题! :) :)

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

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