我是新手,正在尝试获取最后插入的记录 id。
但在这里它给了我以下错误:
错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“SELECT @p_customerId”附近使用正确的语法
任何想法如何解决这个问题。
谢谢,
<?php
class MyConnection{
var $db_host = '127.0.0.1';
var $db_user = 'root';
var $db_password = 'xxx';
var $db_name = 'xxx';
var $connection;
function Connect(){
$this->connection = @mysqli_connect($this->db_host, $this->db_user, $this->db_password)
or
die("Error: ".mysqli_connect_error());
mysqli_select_db($this->connection, $this->db_name);
mysqli_query($this->connection, "SET NAMES 'utf8'");
}
function Disconnect(){
mysqli_close($this->connection);
}
function ExecSQL($query){
$result = mysqli_query($this->connection, $query) or die('Error: '.mysqli_error($this->connection));
return $result;
}
function LastInsertId() {
return mysqli_insert_id();
}
}
if (isset($_POST['btnSubmit'])) {
$v_custname = mysql_real_escape_string($_POST['txtcustname']);
$v_custphone = mysql_real_escape_string($_POST['txtcustphone']);
$v_custemail = mysql_real_escape_string($_POST['txtcustemail']);
$conn = new MyConnection();
$conn->Connect();
$conn->ExecSQL("CALL sp_customer('$v_custname','$v_custphone','$v_custemail',@p_customerId); SELECT @p_customerId;");
echo $conn->LastInsertId();
$conn->Disconnect();
}
?>
这是程序;
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_customer`(IN `p_customerName` VARCHAR(50), IN `p_customerPhone` VARCHAR(50), IN `p_customerEmail` VARCHAR(50), OUT `p_customerId` INT)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
INSERT INTO customer(CustomerName, CustomerPhone, CustomerEmail)VALUES(p_customerName, p_customerPhone, p_customerEmail);
SELECT LAST_INSERT_ID() as p_customerId;
END