我有一个非常简单的查询,不确定我在这里做错了什么。
我的数据库调用没有收到我期望的 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
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();
}
有问题的行是:$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();
}
还重新启动了 MySQL 服务器以确保那里没有发生任何奇怪的事情。
此外,更新了 SP 以在不使用选择的情况下将随机数据插入表中。
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
更新 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] =>
)
更新 4:
根据我所做的一些研究,除非您使用 mysqli 方法,例如 $this->db->insert(),否则它不会向您提供最后的插入 ID .
我将尝试找出 Ravi 的建议,但代码点火器似乎不允许显示的示例。至少我现在知道我并不疯狂,这只是不正常的行为,除非你使用 ``insert` 方法而不是存储过程。
最佳答案
This answer可能会解释为什么您现有的代码不起作用。引用:
CodeIgniter's
insert_id()will only return an ID of aninsert(). Unless you are executing something like$this->db->insert('table', $data);before calling the function it will not be able to return an 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
然后使用类似下面的方法获取输出参数值:
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;
}
如果上述方法不起作用,请尝试添加 $this->db->trans_start(); 和 $this->db->trans_complete(); 在存储过程调用之前和之后确保事务已提交。
关于php - MySQL 存储过程没有返回插入 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48247398/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
所以我开始关注ruby,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r