【发布时间】:2013-12-09 03:02:07
【问题描述】:
我想通过一个代码使用 NOT EXISTS 和 last_insert_id() 来执行以下操作。 下面的代码满足了我的需求,Col1 是一个唯一键,如果数据已经存在,代码将不会插入数据。如果插入新行或已存在新行,则始终代码返回相关行的 $id。
<?php
mysql_query(" INSERT table SET col1='data1', col2='data2' ");
$result = mysql_query("SELECT id FROM table WHERE col1='data1' ") ;
while($row = mysql_fetch_array( $result )) { $id=$row['id'];}
header("Location:page.php?id=$id");
?>
【问题讨论】:
-
如果它满足您的需求,您的问题是什么?
-
@altafhussain 我想通过一个代码使用 NOT EXISTS 和 last_insert_id() 来提高性能
标签: php mysql insert not-exists last-insert-id