English 中文(简体)
Php PDO 多次编写交易报表
原标题:Php PDO multiple prepare statements in transaction
  • 时间:2012-04-10 11:28:51
  •  标签:
  • php
  • mysql
  • pdo

我有这一法典:

    $this->db->beginTransaction();
    $query =  INSERT INTO `table1` VALUES (NULL,:a,:b,NULL,NOW()) ;
    $sth = $this->db->prepare($query);
    foreach ($values as $k => $v) {
      $sth->bindParam( :  . $k, $v, PDO::PARAM_INT);
    }

    $this->executeQueryRollbackOnException($sth,  Message_1 );
    $resetId = $this->db->lastInsertId();

    $query =  UPDATE `table2` SET c=:c,d=:d WHERE reset_id IS NULL ;
    $sth = $this->db->prepare($query);
    foreach ($values2 as $k => $v) {
      $sth->bindParam( :  . $k, $v, PDO::PARAM_INT);
    }


    $this->executeQueryRollbackOnException($sth,  Message_2 );

    Zend_Debug::dump($sth->rowCount(),  Affected ); // This is 0

    // Commit
    $this->db->commit();    

......

  private function executeQueryRollbackOnException($sth, $message) {
    try {
      $sth->execute();
    } catch (Exception $e) {
      $this->logSQLError($e);
      $this->db->rollBack();
      throw new Exception($message);
    }
  }

第一个问题已经执行,但第二个没有执行。 没有产生我的错误。 任何想法?

最佳回答

我找到了解决办法。

foreach ($values as $k => $v) {
   $sth->bindParam( :  . $k, $v, PDO::PARAM_INT);
}

foreach ($values as $k => $v) {
   $sth->bindParam( :  . $k, $values[$k], PDO::PARAM_INT);
}

索赖斯·伊登首次张贴了正确的法典,但我排除了简化法典的征兆。

问题回答

The fact that $sth->rowCount() is 0 does not mean the query was not run. If you have no error reported in your PHP error log, then the query ran just fine. If the row count is 0, that means that no rows were updated. This is very likely due to your condition not matching any rows.

回答如下:如果你收到0份,那么你知道问题是<条码>。 UPDATE conditions:

SELECT COUNT(*) FROM `table2` WHERE reset_id IS NULL




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签