I'm recently have learned to use Zend Framework. I did a simple CRUD application. But now I want to use a existing database for a more complex application and I want to know how I call a stored procedure in the Model, how to send parameters, how to read the results and store them in an array in PHP. Please. I appreciate any kind of help :)
相关问题
- What is the best way to cache a table from a (SQL)
- Zend Auth locked session
- Zend/PHP: Problem uploading/downloading file to/fr
- SQL [Conversion to bool]
- Executing a stored procedure using a DbConnection
相关文章
- SQL Server 2008: Joining results of STORED PROCEDU
- Is SaveChanges() Necessary with Function Imports (
- Can I lazy load a navigation property by delegatin
- Possible disadvantages of Zend [closed]
- how to pass two parameters to call a stored proced
- INSERT INTO with exec with multiple result sets
- Zend Framework Modules with common resources
- How to store output of a stored procedure on to di
It's not too hard. Here's an example of a MySQL stored procedure with an
IN
parameter, anOUT
parameter, and a result set:You can call this with the
query()
method, and pass a parameter:The trick is that MySQL stored procs might return multiple result sets (if the proc had multiple
SELECT
queries for instance). So the API must advance through all result sets before you can execute another SQL query. Or else you get the "Commands out of sync" error.If you use the PDO_MySQL adapter:
If you use the MySQLi adapter, you'll find that
Zend_Db_Statement_Mysqli
doesn't implementnextRowset()
, so you have to call the internal mysqli connection object:Once you clear the result sets, you can run subsequent SQL queries, for example to fetch the value of the procedure's
OUT
parameter:Great answer from Bill. Just for completeness, if you encounter:
When using this method to get a result set from your procedure, check your arguments. I refactored a method and was passing NULLs as arguments to the procedure as the variables I'd used were out of scope. Once I'd fixed this silly mistake the problem went away (to be replaced by another):
I'm using
$stmt->fetchAll()
though. I switched to usingprepare()
andexecute()
in place ofquery()
. Switching tomysqli
frompdo_mysql
in my Zend_Db config finally got things working for me. I found this information from the following SO question:Call Multiple Stored Procedures with the Zend Framework
If someone is looking for
ZendFramework 2
\Zend Expressive
usingZend\Db
:There is another way to do this using
createStatement()
method.