PDOStatement :: execute()返回true,但数据未更新 [英] PDOStatement::execute() returns true but the data is not updated

查看:243
本文介绍了PDOStatement :: execute()返回true,但数据未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,这个准备好的查询不起作用:以下php代码正确(没有任何警告或通知),但是相应的数据未更新.对于类似的更新,数据库已正确更新,但不适用于此表.

I don't know why, this prepared query doesn't work: The following php code is correct (not any warning or notice), but the correponding datas are not updated. For similar update the db are correctly updated, but not for this table.

<?php

  error_reporting(E_ALL);
  ini_set('display_errors', 'On');
  $pdo = new PDO('mysql:dbname=test;host=localhost', 'root', ''); 
  $sql = 'UPDATE `ml_user` SET `username` = :username, `password` = :password, `email` = :email, `active` = :active, `last_login` = :last_login WHERE `id_user` = :id_user';
  $sth = $pdo->prepare($sql);
  var_dump($sth); // object(PDOStatement)#39 (1) { ["queryString"]=> string(165) "UPDATE `ml_user` SET `username` = :username, `password` = :password, `email` = :email, `active` = :active, `last_login` = :last_login WHERE `id_user` = :id_user" }
  $datas = array(
    ':id_user' => 1, ':username' => 'my username',
    ':password' => 'ae25ff724d069dcb1a7fff05616ad6abc1',
    ':email' => 'username@example.com',
    ':active' => 1, ':last_login' => 1382990654,
  );
  $res = $sth->execute($datas);
  var_dump($res); // bool(true) 

sql表

CREATE TABLE `ml_user` (
 `id_user` int(11) NOT NULL AUTO_INCREMENT,
 `active` tinyint(1) NOT NULL DEFAULT '0',
 `email` varchar(127) NOT NULL,
 `username` varchar(32) NOT NULL DEFAULT '',
 `password` char(50) NOT NULL,
 `logins` int(10) unsigned NOT NULL DEFAULT '0',
 `last_login` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id_user`),
  UNIQUE KEY `uniq_username` (`username`),
  UNIQUE KEY `uniq_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

推荐答案

execute()返回 true 时,表示该语句成功",但这并不意味着它已更改.这只是意味着没有错误.

When execute() returns true it means the statement was "successful," but it doesn't mean it changed anything. It just means there was no error.

  • 由于WHERE子句中的条件,它可能具有匹配的零行.那仍然被认为是成功".您可以尝试使用具有相同WHERE子句的SELECT并获取结果,以确认它与行匹配.

  • It may have matched zero rows because of conditions in the WHERE clause. That's still considered a "success." You can try a SELECT with the same WHERE clause, and fetch the results, to confirm that it matches rows.

它可能已经匹配了一个或多个行,但是您设置的值已经是那些行上的值.您可以在执行后调用$sth->rowCount(),以查找UPDATE 受影响的行数(这可能少于它匹配的行数).

It may have matched one or more rows, but the values you are setting are already the values on those rows. You can call $sth->rowCount() after you execute, to find out how many rows the UPDATE affected (this may be less than the number of rows it matched).

如果该表有多个副本,则应再次检查是否已在要读取的数据库中进行了更改.有时候我会遇到这种情况-我忘记了更改应用程序配置文件,但我不知道自己正在更新错误的数据库.

If you have more than one copy of this table, you should double-check that the change has been made in the database you are reading. It happens to me sometimes -- I forget to change an application configuration file, and I don't realize I'm updating the wrong database.

这原来是问题所在-第三点的变化:

  • 如果您在多个服务器上工作,请再次检查您是否正在正确的mysql服务器上检查更改.

这篇关于PDOStatement :: execute()返回true,但数据未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆