I execute two queries one after another: one is INSERT another one is SELECT that selects the inserted row.
Although the row was inserted successfully (i can see it in the db) the select query fails to return the row.
When I execute the SELECT query again it returns the correct result.
Insert:
$stmt = $pdo->prepare('INSERT INTO user (id ,name, lastname ,birthday, social_type, social_id) VALUES(NULL, :name, :lastname, :birthday, :social_type, :social_id)');
$success=$stmt->execute(array(
':name' => $user['name'],
':lastname' => $user['lastname'],
':birthday' => $user['birthday'],
':social_type' => $user['social_type'],
':social_id' => $user['social_id']
));
Select
$stmt = $pdo->prepare('SELECT * FROM user WHERE social_id = :social_id AND social_type = :social_type LIMIT 1');
$stmt->execute(array(
'social_id' => $user['social_id'],
'social_type' => $user['social_type'] ));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
如果你正使用InnoDB:
如果你正使用InnoDB,因为你验证一行被插入,它应该已返回与SELECT,只要SELECT查询了所插入的实际行的关键。 (你确定你使用的不是功能,就如同INSERT延误吗?这可能会阻止该行被退回。)
如果使用的是MYISAM:
由于MyISAM不支持事务,则选择应该返回插入,但我无法找到任何东西,指出这实际上是保证。
注:如果您使用的是MYISAM(根据此链接默认)下面列出的第一个URL指出,插入将锁定表。 然而,第二URL指出锁放置在由插入件是可锁定,以便不应该阻止表被读取。
http://www.sitepoint.com/mysql-mistakes-php-developers/
http://aarklondatabasetrivia.blogspot.com/2009/04/how-to-lock-and-unlock-tables-in-mysql.html
如果你正使用InnoDB(续):
如果AUTOCOMMIT是在你的系统中使用(我不知道),你应该已经看到所选行(这个问题指出插入的行被验证为已被添加到数据库中)。
如果交易是在使用过程中,交易必须一直致力于(这个问题指出插入的行被验证为已被添加到数据库中)。
你肯定是第一次执行SELECT查询是一样的一个第二次?
你确定$user['social_id']
在INSERT后在选择的时间相同的值?
如果你是指在会话中插入另一个事务,而不是一行在做刀片,那么这个网址:
http://blogs.innodb.com/wp/2011/04/get-started-with-innodb-memcached-daemon-plugin/
指出:“你需要做的‘读未提交的’选择,找到刚刚插入的行:”
IE设置的会话事务隔离级别读取未提交;
http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html
(此功能可以依赖于使用中的版本的MySQL)
如果由于某种原因,你正在使用INSERT DELAYED,该行可能不会返回
注:根据这个URL,如果你已经开始交易,选择的行表示,在未来的SELECT语句(而不是在PHP):
http://zetcode.com/databases/mysqltutorial/transactions/
这种说法意味着,如果你开始一个事务,则无需设置AUTOCOMMIT:
“MySQL也自动提交不属于交易的一部分陈述”。
此网址介绍如何在PHP启动一个事务:
PHP + MySQL的交易实例
文章来源: Is it possible to INSERT and then SELECT the inserted row one after another?