简体   繁体   English

PHP PDO查找找到的UPDATE行

[英]PHP PDO finding found rows of a UPDATE

I have a small problem that I need help solving. 我有一个小问题需要解决。 Its more a lack in my MySQL knowledge then a problem really... 我的MySQL知识更加缺乏,然后才是问题……

I have a table of employes something like this: 我有一张这样的员工表:

CREATE TABLE employes (CoNo int, EmplCode int, Name varchar(250), Dep varchar(4))

It contains some data as such: 它包含一些数据,例如:

3 123 'John Jackson' 'INFO'
3 124 'Simon Says'   'SELL'
...

I am trying to update Simons row using PHP PDO (something like): 我正在尝试使用PHP PDO更新Simons行(类似):

$sql = "UPDATE employes SET CoNo = 3, Name = 'Simon Says', Dep = 'SELL' WHERE EmplCode = 124";
$stmt = $PDOObject->prepare($sql);
$status = $stmt->execute();
echo $stmt->rowCount();

Now lets say everything is connecting and everything is working. 现在说一切都在连接,一切都在工作。 If you paid attention during your MySQL classes you will soon see that this will echo 0 rows affected. 如果您在MySQL上课时留意了一下,您很快就会发现这将回显受影响的0行。

Which apparently is a normal behavior for MySQL because the UPDATE didn't change anything to the actual data in the table. 这显然是MySQL的正常行为,因为UPDATE并没有改变表中的实际数据。

But what I need is a function just like rowCount() that will give me the FOUND rows not the AFFECTED rows or something that will let me force the update on the MySQL table. 但是我需要的是一个类似于rowCount()的函数,它将为我提供FOUNDED行而不是AFFECTED行,或者让我在MySQL表上强制进行更新的函数。

I have looked and googled the thing but I can't find anything and I am worried that it might be because it doesn't exist. 我已经查看并搜索了该东西,但找不到任何东西,我担心这可能是因为它不存在。

I have heard of something called FLAG_FOUND_ROWS but I am not sure it is a PDO flag or how to use it (and I cant find anything on it). 我听说过一个叫做FLAG_FOUND_ROWS的东西,但是我不确定这是一个PDO标志或如何使用它(我在上面找不到任何东西)。

What I was hoping to achieve with this is something like a REPLACE statement or an UPSERT without the need of a primary key. 我希望通过这种方式实现的是不需要主键的REPLACE语句或UPSERT之类的东西。

If UPDATE return 0 rows then INSERT the row in the table. 如果UPDATE返回0行,则在表中插入该行。

EDIT 编辑

Sorry for the mistake I forgot the WHERE clause in the query. 对不起,我忘记了查询中的WHERE子句。 As pointed out in the comments. 正如评论中指出的那样。

EDIT 编辑

I am still doing research on google for this and found a MySQL function called FOUND_ROWS() but It seems it need to be use in a separate query which doesn't really help since I could just do a SELECT beforehand. 我仍在为此进行Google的研究,并发现了一个名为FOUND_ROWS()的MySQL函数,但它似乎需要在单独的查询中使用,但这并没有真正的帮助,因为我可以事先做一个SELECT。 But this might give you guys an idea that I haven't think of yet. 但这可能会给你们一个我尚未想到的想法。

Thank you for all the help guys (and girls). 谢谢大家(和女孩)的帮助。

It seems that PDO::MYSQL_ATTR_FOUND_ROWS is a mysql connection option. 似乎PDO :: MYSQL_ATTR_FOUND_ROWS是mysql连接选项。 It works only as PDO connection option as well, like 它也仅用作PDO连接选项,例如

$PDOObject = new PDO($dsn,DB_USER,DB_PASS, array(PDO::MYSQL_ATTR_FOUND_ROWS => TRUE));

after this, rowCount returns the number of found rows, not affected. 之后, rowCount返回发现的行数,不受影响。

echo $stmt->rowCount();

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

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