SBB*_*SBB 7 php mysql codeigniter
我有一个非常简单的查询,不知道我在这里做错了什么.
我的数据库调用没有insert id像我期望的那样收到.
表:
存储过程:
CREATE DEFINER=`root`@`localhost` PROCEDURE `addCustomerProduct`(IN in_customerID INT, in_productID INT)
BEGIN
INSERT INTO order_customer_product (customerID, productID, retailAmountAtPurchase, faceValue)
SELECT
in_customerID,
in_productID,
p.retail,
p.faceValue
FROM
products as p
WHERE
p.productID = in_productID;
END
Run Code Online (Sandbox Code Playgroud)
PHP:
public function addProduct($data, $userID)
{
// Do we already have a pending order for this user?
$orderID = $this->doesOrderExist($userID);
// We had no order, lets create one
if (!$orderID) {
$orderID = $this->createOrder($userID);
}
/**
* Insert the customer product.
* This relates a denomination to a customer.
*/
$customerProductID = $this->addCustomerProduct($data);
// Add this customer product to the order
$this->addProductToOrder(array("customerProductID" => $customerProductID, "orderID" => $orderID));
// Return
return $customerProductID;
}
/**
* Description: Add a customer product / reward
* Page: client/add_reward
*/
public function addCustomerProduct($data){
$procedure = "CALL addCustomerProduct(?,?)";
$result = $this->db->query($procedure, $data);
return $this->db->insert_id();
}
Run Code Online (Sandbox Code Playgroud)
问题的关键是:$customerProductID = $this->addCustomerProduct($data);.
正在将新记录插入表中,表中有一个PK/AI.数据很好但是0作为返回$customerProductID.
select语句中的插入是否会返回insert ID?
更新@Ravi-
更新2:
我创建了一个单独的方法,并对发送的查询和数据进行了硬编码.
它添加的记录很好,AI上升,0返回为last id.
public function test(){
$procedure = "CALL addCustomerProduct(?,?)";
$result = $this->db->query($procedure, array("customerID" => 1, "productID" => 20));
echo $this->db->insert_id();
}
Run Code Online (Sandbox Code Playgroud)
还重新启动了MySQL服务器,以确保那里没有任何奇怪的事情.
此外,更新SP只是将随机数据插入表中而不使用select.
CREATE DEFINER=`root`@`localhost` PROCEDURE `addCustomerProduct`(IN in_customerID INT, in_productID INT)
BEGIN
INSERT INTO order_customer_product (customerID, productID, retailAmountAtPurchase, faceValue)
VALUES(8,2,'4.55',25);
END
Run Code Online (Sandbox Code Playgroud)
更新3:
在插入之后,我打印出最后运行的查询以及结果.您会注意到有1个受影响的行(插入正在发生)但insert_id仍为0.
CALL addCustomerProduct('8','33')
CI_DB_mysqli_result Object
(
[conn_id] => mysqli Object
(
[affected_rows] => 1
[client_info] => mysqlnd 5.0.12-dev - 20150407 - $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $
[client_version] => 50012
[connect_errno] => 0
[connect_error] =>
[errno] => 0
[error] =>
[error_list] => Array
(
)
[field_count] => 0
[host_info] => Localhost via UNIX socket
[info] =>
[insert_id] => 0
[server_info] => 5.6.35
[server_version] => 50635
[stat] => Uptime: 1637 Threads: 3 Questions: 508 Slow queries: 0 Opens: 113 Flush tables: 1 Open tables: 106 Queries per second avg: 0.310
[sqlstate] => 00000
[protocol_version] => 10
[thread_id] => 25
[warning_count] => 0
)
[result_id] => 1
[result_array] => Array
(
)
[result_object] => Array
(
)
[custom_result_object] => Array
(
)
[current_row] => 0
[num_rows] =>
[row_data] =>
)
Run Code Online (Sandbox Code Playgroud)
更新4:
从我做过的一些研究中,除非你使用mysqli方法,否则$this->db->insert()它不会向你提供最后一个插入ID.
我将尝试找出Ravi的建议,但似乎代码点火器不允许显示的示例.至少我现在知道我并不疯狂,除非你使用``insert`方法和存储过程,否则它不是正常的行为.
这个答案可以解释为什么您现有的代码不起作用.报价:
CodeIgniter
insert_id()只会返回一个IDinsert().除非您$this->db->insert('table', $data);在调用函数之前执行某些操作,否则它将无法返回ID.
MySQL LAST_INSERT_ID();应该在这里帮助你(假设你有权改变存储过程定义).将其更改为:
CREATE DEFINER=`root`@`localhost` PROCEDURE `addCustomerProduct`(
IN in_customerID INT, in_productID INT, OUT out_customerProductID INT)
BEGIN
INSERT INTO order_customer_product (
customerID, productID, retailAmountAtPurchase, faceValue)
VALUES(8,2,'4.55',25);
SELECT LAST_INSERT_ID() INTO out_customerProductID;
END
Run Code Online (Sandbox Code Playgroud)
然后使用类似以下内容来获取输出参数值:
public function addCustomerProduct($data) {
$procedure = "CALL addCustomerProduct("
. $this->db->escape($data["customerID"]).", "
. $this->db->escape($data["productID"]).", "
. "@customerProductID);"
$this->db->query($procedure);
$query = $this->db->query("SELECT @customerProductID AS customerProductID");
if($query->num_rows() > 0)
return $query->result()->customerProductID;
else
return NULL;
}
Run Code Online (Sandbox Code Playgroud)
如果上述方法无效,尝试添加一个$this->db->trans_start();与$this->db->trans_complete();之前和存储过程的调用之后,以确保承诺的交易.
理想情况下,以下行应该有效
$this->db->insert_id;
Run Code Online (Sandbox Code Playgroud)
但是,我不确定为什么不起作用,所以我建议采用以下解决方法,使用附加参数重新编译您的过程out_lastId,这将返回最后插入的 id
CREATE DEFINER=`root`@`localhost` PROCEDURE `addCustomerProduct`(IN in_customerID INT, in_productID INT, OUT out_lastId INT)
Run Code Online (Sandbox Code Playgroud)
并且,插入后设置最后插入的 id 的值。
SET out_lastId = LAST_INSERT_ID();
Run Code Online (Sandbox Code Playgroud)
==更新==
$this->db->multi_query( "CALL addCustomerProduct($data, @id);SELECT @id as id" );
$db->next_result(); // flush the null RS from the call
$rs=$this->db->store_result(); // get the RS containing the id
echo $rs->fetch_object()->id, "\n";
$rs->free();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
895 次 |
| 最近记录: |