INSERT

时间:2017-02-17 11:15:22

标签: php mysql pdo transactions

我有一个PHP PDO事务而不是插入行然后选择它。 INSERT操作完成后,我调用另一个执行SELECT的函数。问题是SELECT在被调用函数内执行时不会返回先前插入的行。

在这里你有代码:

class FinderMyTable {

  function myfunction(...) {
    $db = DatabaseManager::getInstance()->getConnection();
    $db->beginTransaction();
    try {

      $dbParameters = array(':var1' => $var1, ...);
      $dbStatement  = $db->prepare('INSERT INTO MyTable(...) VALUES(:var1, ...)');
      $success = $dbStatement->execute($dbParameters);
      if (!$success) throw new Exception('internal-database-error');

      $insertedRowId = $db->lastInsertId();

      $dbParameters = array(':insertedRowId' => $insertedRowId);
      $dbStatement  = $db->prepare('SELECT * FROM MyTable WHERE id = :insertedRowId');
      $dbStatement->execute($dbParameters);
      $row = $dbStatement->fetch();

      error_log('ROW: ' . print_r($row, true) );

      $rowData = $this->obtainRowData($insertedRowId);

      $db->commit();

    } catch (PDOException $e) {
      $db->rollBack();
      error_log($e->getMessage());
      throw new Exception('internal-database-error');
    } catch (Exception $e) {
      $db->rollBack();
      error_log($e->getMessage());
      throw $e;
    }
  }

  function obtainRowData($insertedRowId) {
    $db = DatabaseManager::getInstance()->getConnection();

    $dbParameters = array(':insertedRowId' => $insertedRowId);
    $dbStatement  = $db->prepare('SELECT * FROM MyTable WHERE id = :insertedRowId');
    $dbStatement->execute($dbParameters);
    $row = $dbStatement->fetch();

    error_log('ROW INSIDE FUNCTION: ' . print_r($row, true) );

    $data = $row['field1'];
    // ...
    return $data;
  }

}

正如您所看到的,我已拨打了2 error_log(...);次来电 “ROW”调用返回已插入的正确数据:

ROW: Array
(
    [id] => 813
    [0] => 813
    [field2] => 289
    [1] => 289
    [field3] => value
    [2] => value
    [field4] => value2
    [3] => value2
)

“ROW INSIDE FUNCTION”调用返回不正确的数据:

ROW INSERTED FUNCTION: Array
(
    [id] => 2345
    [0] => 2345
    [comment] =>
    [1] =>
)

正如您所看到的,上次调用中获得的值都不会与插入的行相对应。此外,我有一个“注释”字段,该字段未出现在此表中!

下面是我的DatabaseManager类,它是一个管理PDO连接的Singleton类:

final class DatabaseManager {

  private static $instance = null;
  private $connection;

  private function __construct() {
    try {
      $connection = new PDO('mysql:host=...;dbname=...;charset=latin1', 'user', 'pass');
      $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
      error_log($e->getmessage());
      die();
    }
    $this->connection = $connection;
  }

  public function __clone() {}

  public static function getInstance() {
    if (self::$instance === null) self::$instance = new DatabaseManager();
    return self::$instance;
  }

  public function getConnection() {
    return $this->connection;
  }

}

可能是什么问题? 为什么被调用函数选择一个完全不同的行?

1 个答案:

答案 0 :(得分:-2)

解决!好的,我简化了代码......在error_log(...);$row之间有一些代码行,其中一行覆盖了error_log(...)变量。因此,两个SELECT从一开始就给出了相同的结果,NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0]; [self loadFileURL:request.URL allowingReadAccessToURL:documentsURL]; 是在覆盖变量上完成的。