SELECT LAST_INSERT_ID()在直接的MySQL查询中有效,但在PHP查询中不稳定。
SELECT LAST_INSERT_ID()在直接的MySQL查询中有效,但在PHP查询中不稳定。
请注意,当我写这段代码时,它在MSSQL数据库上运行得很好,现在我正在使用SELECT LAST_INSERT_ID()来替代@SCOPE_IDENTITY在MySQL上写同样的代码。我有一段代码,用于输入公司和该公司的客户详细信息。第一个SELECT LAST_INSERT_ID()用于将companies表中的company_id插入到clients表中,但在检索client_id时出现错误。以下是代码:
//// 设置初始SQL
$SQLINSERTCOMPANY = "insert into companies(company_name,is_organisation,company_regno,company_url,is_active,entry_date) VALUES ('$company_name',$is_organisation,'$company_regno','$company_url',1,'$entry_date');"; $SQLINSERTCOMPANYDETAILS = "insert into clients (client_name,client_tel,client_mobile,client_email,client_usern,client_pwdx,mailing_list,savedsession,salt,company_id) VALUES ('$client_name','$client_tel','$client_mobile','$client_email','$client_email','$psw',$sign_up,'$PHPSESSIONID','$salt'"; $SQLUPDATECOMPANY = "update clients set company_name = '$company_name', is_organisation = $is_organisation, company_regno = '$company_regno', company_url = '$company_url', is_active = 1, entry_date = '$entry_date' "; $SQLUPDATECOMPANYDETAILS = "update clients set client_name = '$client_name', client_tel = '$client_tel', client_mobile = '$client_mobile', client_email = '$client_email', client_usern = '$client_email', client_pwdx = '$psw', mailing_list = $sign_up, savedsession = '$PHPSESSIONID' "; //// 如果购物车中有商品,获取购物车以备后用 $cart = $_SESSION['cart']; if($cart){ $strSQL = "select * from category_items where category_item_id IN ($cart) order by category_items.item_code;"; $query_get_value = mysql_query($strSQL) or die ('query failed ' . mysql_error()); } ///// 在这里添加新客户 if($_POST['action'] == "add"){ ////// 插入新公司或只更新详细信息 if($no_of_client_entry == 0) { ////// 更新clients表 $strSQL1 = $SQLINSERTCOMPANY; ////// 插入客户详细信息 $strSQL2 = $SQLINSERTCOMPANYDETAILS . ",(SELECT LAST_INSERT_ID())); SELECT LAST_INSERT_ID() as NEWID;"; $sent_to = $client_email; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $headers .= "FROM: TheImageLounge"; $subject = "Welcome to The Image Lounge"; $message = " Error Report "; $message .= ""; $message .= ""; $message .= "Welcome to Website... TBC/p>"; $message .= "Your Details Are:"; $message .= "Your username: $client_email"; $message .= "Your password: " . $_SESSION['client_psw'] . ""; $message .= "We hope that you will enjoy using our website."; $message .= "Thank You and best regards,"; $message .= "The Website."; $message .= ""; $sentOK = mail($sent_to,$subject,$message,$headers); } else { ///// 只更新客户详细信息 $client_id = $clients_rows['client_id']; ////// 更新clients表 $strSQL1 = $SQLUPDATECOMPANY . " where client_id = $client_id;"; ////// 插入客户详细信息 $strSQL2 = $SQLUPDATECOMPANYDETAILS . " where client_id = $client_id; SELECT client_id as NEWID from clients where client_id = $client_id;"; } ///// 结束if ///echo $strSQL2; ///// 执行事务 echo $strSQL1 . ""; echo $strSQL2; $query_insert_client = mysql_query($strSQL1) or die('' . mysql_error()); $query_insert_clients = mysql_query($strSQL2) or die('' . mysql_error()); $last_client = mysql_fetch_assoc($query_insert_clients); $client_id = $last_client['NEWID']; mysql_free_result($query_insert_clients);
现在,当我输出SQL时,它的格式如下:
insert into companies(company_name,is_organisation,company_regno,company_url,is_active,entry_date) VALUES ('Individual',0,'','',1,'2011-06-08 14:16:33'); insert into clients (client_name,client_tel,client_mobile,client_email,client_usern,client_pwdx,mailing_list,savedsession,salt,company_id) VALUES ('Andis','0777','','andrew@com','andrew@com','6be86ab8355979352cdd28bbe7026be71a196936fc1ab0c315629f68c0d5f9162acd20b201a27d0ced0cfd53a8e880a1e8bf3714833ed9c4cbe6d0fe3bf15ca0',0,'8730dfec5ce01b315e1a31007185c486','SALTYDOG',(SELECT LAST_INSERT_ID())); SELECT LAST_INSERT_ID() as NEWID;
但是我得到了以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; SELECT LAST_INSERT_ID() as NEWID' at line 2
如果将输出的SQL复制并粘贴到MySQL中,它可以正常工作。因此,我不确定为什么第二个查询失败。在使用MSSQL数据库时,这个问题没有出现。希望能得到帮助,谢谢。
问题的原因是在尝试一次发送多个查询。根据文档:
mysql_query()只能发送一个查询(不支持多个查询)。
你可能还想考虑使用mysql_insert_id函数来代替手动查询:http://www.php.net/manual/en/function.mysql-insert-id.php
你好,啊,谢谢。我已经研究过这个问题了,但是我想先看看能否自己解决这个问题。MSSQL_QUERY语句可以处理多个查询,所以我以为MYSQL_QUERY也可以。
解决方法:使用mysql_insert_id函数来获取最后插入的ID。这个函数会返回上一次插入操作所生成的AUTO_INCREMENT的ID值。使用这个函数可以避免执行额外的查询来获取最后插入的ID。
示例代码:
$query = "INSERT INTO table (column1, column2) VALUES ('value1', 'value2');"; $result = mysql_query($query); if ($result) { $lastInsertedId = mysql_insert_id(); echo "Last inserted ID: " . $lastInsertedId; } else { echo "Error: " . mysql_error(); }
问题的原因是在PHP查询中,不能在同一个函数中同时执行两个MySQL SQL语句。解决方法是使用PHP函数mysql_insert_id()来获取最后插入的ID。
以下是整理好的文章:
在使用PHP查询数据库时,有一个问题困扰了我。问题是,我在使用MySQL查询时,可以使用"SELECT LAST_INSERT_ID()"来获取最后插入的ID,但是当我将这个查询放在PHP代码中时,却无法正常工作。
下面是我遇到的错误代码:
error: 1. u did not close the insert tag just do simple change $addorder = "INSERT INTO table_name () VALUES ()"; $id = "SELECT LAST_INSERT_ID() as new_id"; mysql_query($addorder) or die("not added in to the orders db:<br />".mysql_error()."<br />".$addorder."<hr />"); echo($id);
根据错误提示,我确实没有关闭插入标签。但是,根据一些回答的说法,我不能在同一个函数中同时执行两个MySQL SQL语句。我认为我可以尝试使用另一种解决方法,即使用PHP函数mysql_insert_id()来获取最后插入的ID。
通过使用mysql_insert_id()函数,我可以在插入数据后立即获取到最后插入的ID。下面是我修改后的代码:
$addorder = "INSERT INTO table_name () VALUES ()"; mysql_query($addorder) or die("not added in to the orders db:<br />".mysql_error()."<br />".$addorder."<hr />"); $id = mysql_insert_id(); echo($id);
通过这种方式,我成功地解决了问题。另外,我还了解到在使用MSSQL查询时,可以使用MSSQL_Query函数来实现同样的功能。
通过这个问题和解决方法的学习,我对PHP查询中的一些限制有了更深入的了解,并学会了如何使用mysql_insert_id()函数来获取最后插入的ID。希望这篇文章对其他遇到相同问题的人有所帮助。