简体   繁体   English

如何在php中调用存储过程?

[英]How do i call stored procedure in php?

I want to use stored procedure for insert statement in php. 我想将存储过程用于php中的insert语句。

I have created a sp and now i want to call the same in my php file. 我创建了一个sp,现在我想在我的php文件中调用它。

Below is my sp: 下面是我的sp:

DELIMITER $$
DROP PROCEDURE IF EXISTS `radiolocous`.`insert_gmr`$$
CREATE PROCEDURE `storedproc_test`.`insert_gmr`
(
IN version varchar(250),
IN visitorId VARCHAR(250),
IN dwellTime VARCHAR(250),
IN poiId VARCHAR(250),
IN srId VARCHAR(250),
IN zoneId VARCHAR(250),
IN poiProximityConfidence VARCHAR(250),
IN zoneProximityConfidence VARCHAR(250),
IN poiPresenceConfidence VARCHAR(250),
IN zonePresenceConfidence VARCHAR(250),
IN normalizedTime VARCHAR(250)
)
BEGIN


INSERT INTO gmr(version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime)
values(version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime);
END $$
DELIMITER;

My insert statement is something like: 我的插入语句类似于:

$result = db_query("INSERT INTO gmr(version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime)
                                VALUES ('" . implode("', '", $array) . "')")
                                or die('Connect Error: ' . mysqli_connect_error());

How do i call the sp in my php, since i receive the values in a json, i not inserting it manually, but i have given sp parameter as IN 我如何在PHP中调用sp,因为我在json中收到值,所以我没有手动插入它,但是我将sp参数指定为IN

Is this correct? 这个对吗?

I tried like: 我尝试过:

$result = db_query("CALL insert_gmr('" . implode("', '", $array) . "')") or die('Connect Error: ' . mysqli_connect_error());

I get Connect error without any description. 我收到没有任何描述的连接错误。

Is the problem in my sp or the way im calling in my php ? 是我的sp中的问题还是我在我的php中调用的方式?

I think your mistake is on the way you try to call your procedure. 我认为您的错误在于尝试调用过程的方式上。

In your case you should do something like that : 在您的情况下,您应该执行以下操作:

$result = db_query("CALL insert_gmr('".implode("', '", $array). "')"))

This way your call is correct as long as there are as many values in $array that there are parameters in your stored procedure 这样,只要$ array中的值与存储过程中的参数一样多,调用就正确了

To know more about using stored procedure with PHP you can refer to the documentation : http://php.net/manual/en/mysqli.quickstart.stored-procedures.php 要了解有关在PHP中使用存储过程的更多信息,请参考文档: http : //php.net/manual/en/mysqli.quickstart.stored-procedures.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM