I have a table CLIENT with client_id
as auto increment. Now i need to populate other tables SERVICE, COSTUMERS and STOCK at same time. Problem is that i need to enter the newly generated client_id
from CLIENT in other tables too.
我有一个表CLIENT,client_id为自动增量。现在我需要同时填充其他表SERVICE,COSTUMERS和STOCK。问题是我还需要从其他表中的CLIENT输入新生成的client_id。
Right now what i am doing:
现在我在做什么:
- I insert a row in CLIENT table.
- Fetch MAX(client_id) from that table.
- Put it into others.
我在CLIENT表中插入一行。
从该表中获取MAX(client_id)。
把它放到别人身上。
Here i am worried about consistency as it all depends on guess that MAX(client_id) would be recently generated one. Is there any other way to do so without putting consistency at risk ??
在这里我担心一致性,因为这完全取决于猜测MAX(client_id)最近会生成一个。有没有其他方法可以做到这一点,而不保持一致性?
Help Please
1 个解决方案
#1
3
You may achieve it using
你可以用它来实现它
mysql_insert_id()
It will return the AUTO INCREMENT value in the last query. If last query doesn't generate an auto-increment value, it will return 0.
它将返回上一个查询中的AUTO INCREMENT值。如果最后一个查询没有生成自动增量值,它将返回0。
For further information, consider to take a look at Php.net manual.
有关详细信息,请参阅Php.net手册。
EDIT:
As you mentioned, Mysql is deprecated and to use Mysqli or PDO will be more secure.
正如您所提到的,Mysql已被弃用,使用Mysqli或PDO会更安全。
Procedural style: mysqli_insert_id ( mysqli $link )
程序风格:mysqli_insert_id(mysqli $ link)
Object oriented style: $mysqli->insert_id
面向对象的样式:$ mysqli-> insert_id
$PDO->lastInsertId()
#1
3
You may achieve it using
你可以用它来实现它
mysql_insert_id()
It will return the AUTO INCREMENT value in the last query. If last query doesn't generate an auto-increment value, it will return 0.
它将返回上一个查询中的AUTO INCREMENT值。如果最后一个查询没有生成自动增量值,它将返回0。
For further information, consider to take a look at Php.net manual.
有关详细信息,请参阅Php.net手册。
EDIT:
As you mentioned, Mysql is deprecated and to use Mysqli or PDO will be more secure.
正如您所提到的,Mysql已被弃用,使用Mysqli或PDO会更安全。
Procedural style: mysqli_insert_id ( mysqli $link )
程序风格:mysqli_insert_id(mysqli $ link)
Object oriented style: $mysqli->insert_id
面向对象的样式:$ mysqli-> insert_id
$PDO->lastInsertId()