是否可以将参数绑定到 MYSQLi 中的存储过程? [英] Is it possible to bind parameters to stored procedure in MYSQLi?

查看:34
本文介绍了是否可以将参数绑定到 MYSQLi 中的存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读存储过程和MYSQLi 从这里 http://php.net/manual/en/mysqli.quickstart.stored-procedures.php

I've been reading up on Stored Procedures & MYSQLi from here http://php.net/manual/en/mysqli.quickstart.stored-procedures.php

和其他来源,但我仍然不确定如何使用 调用存储过程?(如准备好的语句)

and other sources, but am still unsure how to call a stored procedure with ? (like a prepared statement)

是否可以将参数绑定到存储过程,类似于以下内容:

$mysqli->query("CREATE PROCEDURE p(IN id_var INT) BEGIN INSERT INTO test(id)
         VALUES(id_var); END;"))
$mysqli->bindParam("i", $some_int);
$mysqli->query("CALL p");

推荐答案

绑定参数是PHP端的操作;存储过程是 MySQL 的东西.两者没有关联,不应如此对待.

Binding parameters is a PHP-side operation; stored procedures are a MySQL thing. The two are not related and should not be treated as such.

通过准备好的语句和参数绑定,您可以使用 prepare() 方法定义查询,然后使用 bind_param() 将值绑定到 ?.因此,鉴于您的示例,准备好的语句方法如下所示:

With prepared statements and parameter-binding, you define a query using the prepare() method, then bind values into the ?s using bind_param(). So given your example, a prepared-statements approach would look like this:

$stmt = $mysqli->prepare("INSERT INTO test (id) VALUES (?)");
$stmt->bind_param('i', $some_int);
$stmt->execute();

另一方面,对于存储过程,您只需将其称为 使用 CALL 命令 作为普通查询:

On the other hand, with a stored procedure, you simply call it using the CALL command as a normal query:

//procedure created beforehand
CREATE PROCEDURE p(IN id_var INT)
INSERT INTO test (id)
VALUES (id_var);
//now in PHP:
$mysqli->query("CALL p($some_int)");

我不确定您是否可以使用 query() 方法运行 CREATE PROCEDURE.

I'm not certain if you can run a CREATE PROCEDURE using the query() method.

所以回答你的问题:是的,可以同时使用两者(因为它们是不同的东西),但不是你想象的那样,而且是多余的.

So to answer your question: yes, it is possible to use both at the same time (because they are different things), but not in the way that you're imagining, and it would be redundant.

这篇关于是否可以将参数绑定到 MYSQLi 中的存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆