我想错乱应该不至于出现,一般数据库对并发控制得比较好!想问问pcdreamam, 这代码行得通吗? <?php mysql_connect("localhost", "mysql_user", "mysql_password") or die("could not connect"); mysql_select_db("mydb"); // mysql_query("INSERT INTO mytable (product) values ('kossu')"); printf ("Last inserted record has id %d\n", mysql_insert_id()); ?>
$sql="INSERT INTO table1(……)VALUES(……)"; mysql_query($sql);$msgID=mysql_insert_id;return $msgID;
<?php mysql_connect("localhost", "mysql_user", "mysql_password") or die("could not connect"); mysql_select_db("mydb"); mysql_query("INSERT INTO mytable (product) values ('kossu')"); printf ("Last inserted record has id %d\n", mysql_insert_id()); ?>这样就行了。记住mysql_insert_id()要用在insert后面,这个函数是通过调用mysql的功能,所以不会需要查询。文档: int mysql_insert_id ( [resource link_identifier]) mysql_insert_id() returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query using the given link_identifier. If link_identifier isn't specified, the last opened link is assumed. mysql_insert_id() returns 0 if the previous query does not generate an AUTO_INCREMENT value. If you need to save the value for later, be sure to call mysql_insert_id() immediately after the query that generates the value. Note: The value of the MySQL SQL function LAST_INSERT_ID() always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries.
mysql_insert_id() 如何使用? 用tablename 作参数吗?
我试了不行啊! 还是需要php高版本支持??
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("could not connect");
mysql_select_db("mydb"); // mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf ("Last inserted record has id %d\n", mysql_insert_id());
?>
mysql_query($sql);$msgID=mysql_insert_id;return $msgID;
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("could not connect");
mysql_select_db("mydb"); mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf ("Last inserted record has id %d\n", mysql_insert_id());
?>这样就行了。记住mysql_insert_id()要用在insert后面,这个函数是通过调用mysql的功能,所以不会需要查询。文档:
int mysql_insert_id ( [resource link_identifier])
mysql_insert_id() returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query using the given link_identifier. If link_identifier isn't specified, the last opened link is assumed. mysql_insert_id() returns 0 if the previous query does not generate an AUTO_INCREMENT value. If you need to save the value for later, be sure to call mysql_insert_id() immediately after the query that generates the value. Note: The value of the MySQL SQL function LAST_INSERT_ID() always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries.
=
select LAST_INSERT_ID()
其实也是查询,只是简化并隐藏了操作罢了