PHP MYSQL:即时选择+插入 [英] PHP MYSQL: instant select + insert

查看:98
本文介绍了PHP MYSQL:即时选择+插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从MYSQL中获取一个值,而不是向前更改该值并插入到mysql中.

I need to pull from MYSQL a value , than change forward the value and insert in to the mysql.

例如:

$str = mysql_fetch_assoc(mysql_query('SELECT `str_id` FROM `ids` ORDER BY `num_id` DESC LIMIT 1 ')); // = aaa
$str = $str++; // aaa -> aab
// than check if "aab" key already exist
// if not , insert as key as "aab"

对于此算法,该脚本有可能从地球的两侧运行,并且两个唯一的密钥将被标记为"aab".

as for this algorithm there is a risk that this script runs from the two sides of the globe and two unique keys will be sign as "aab"..

我该怎么做而又没有两次签名此唯一密钥的风险?

how can i do this without risk of sign this unique key two times?

推荐答案

如果您设置数据库以使该列唯一,则MySQL表将不允许您两次添加相同的值.然后,您可以检查交易结果以确保交易成功.

If you set up your database so that the column is unique, the MySQL table will not allow you to add the same value twice. You can then check the result of the transaction to ensure that it succeeded.

在您的示例中,您似乎只想要一个递增的唯一键.您应该为此使用一个整数,当您插入新行时,该整数会自动递增.如果您希望它为字符串形式,只需对其执行一些基本的数学运算即可.即

From your example it looks like you just want an incremented unique key. You should use an integer for this, which can automatically increment when you insert a new row. If you want it to be in string form just perform some basic math on it. i.e.

1 = a
2 = b
...
27 = aa
28 = ab

这是假设您的表上还没有主键,在这种情况下,我不明白为什么您需要这种形式的字符串.希望这会有所帮助,如果我误解了这个问题,我深表歉意.

This is assuming you don't already have a primary key on your table, in which case I don't understand why you need the string in that form. Hopefully this helps, and I apologize if I misunderstood the question.

这篇关于PHP MYSQL:即时选择+插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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