MySQL插入:先测试? [英] MySQL Insert: Test first?

查看:129
本文介绍了MySQL插入:先测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在将记录插入具有唯一索引的表中时,最好首先测试? 例如

As an example, when inserting a record into a table with a unique index, is it best to test first? e.g.,

$mysqli->query('SELECT email FROM tblUser WHERE email = 'foo@bar.org');

然后确保返回0行,然后

then make sure 0 rows are returned, then do the insert?

$mysqli->query('INSERT INTO tblUser ...');

或者最好是跳过测试,并在有重复条目的情况下处理错误?

Or is it better to just skip the test and handle the error in the event there's a duplicate entry?

THANKS!

推荐答案

键错误。

原因是如果您首先测试,一些其他客户端仍然可以在测试和插入之间的短暂时间插入值。所以你需要处理错误。

The reason is that if you test first, some other client can still insert the value in the brief moment between your test and your insert. So you'd need to handle errors anyway.

这篇关于MySQL插入:先测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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