MySQLi准备语句并替换成 [英] MySQLi prepared statements and REPLACE INTO
问题描述
我必须遵循以下代码:
http://www.nomorepasting.com/getpaste.php?pasteid=22987
如果表中还没有PHPSESSID
,则REPLACE INTO
查询可以正常工作,但是,如果PHPSESSID
存在,则执行调用成功,但是sqlstate设置为'HY000',这不是很有帮助,而$_mysqli_session_write->error
都为空,并且data列不更新.
If PHPSESSID
is not already in the table the REPLACE INTO
query works just fine, however if PHPSESSID
exists the call to execute succeeds but sqlstate is set to 'HY000' which isn't very helpful and $_mysqli_session_write->errno
and
$_mysqli_session_write->error
are both empty and the data column doesn't update.
我相当确定问题出在我的脚本中,因为无论是否在表中PHPSESSID
,从mysql手动执行REPLACE INTO
都可以正常工作.
I am fairly certain that the problem is in my script somewhere, as manually executing the REPLACE INTO
from mysql works fine regardless of whether of not the PHPSESSID
is in the table.
推荐答案
因此,事实证明,使用REPLACE时还有其他我不知道的问题:
So as it turns out there are other issues with using REPLACE that I was not aware of:
错误#10795:REPLACE重新分配了新的AUTO_INCREMENT (根据评论实际上不是错误,而是预期"行为)
Bug #10795: REPLACE reallocates new AUTO_INCREMENT (Which according to the comments is not actually a bug but the 'expected' behaviour)
结果,我的id字段不断增加,因此更好的解决方案是使用以下内容:
As a result my id field keeps getting incremented so the better solution is to use something along the lines of:
在会话中INSERT INTO session(phpsessid,data)VALUES('{$ id}','{$ data}')重复键更新data ='{$ data}'
INSERT INTO session(phpsessid, data) VALUES('{$id}', '{$data}') ON DUPLICATE KEY UPDATE data='{$data}'
这还可以防止破坏任何外键约束并防止潜在的数据完整性问题.
This also prevents any foreign key constraints from breaking and potential causing data integrity problems.
这篇关于MySQLi准备语句并替换成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!