Generic call stored procedure function

P

phpCodeHead

I am trying to write a function that will allow me to call any MySQL
stored procedure and pass any array of values as parameters to pass to
that stored procedure.

So, far I have in my function:

$sql = "CALL $proc_name('" . implode("', '", $params) . "');";

Which is called like:

return callProcedure('someStoredProcedure', $params);

And is working fine, it generates an SQL statment like:

CALL someStoredProcedure('param1', 'param2', '@ID');

PROBLEM IS, it only works fine if all params are strings.

I am trying to come up with a "hack" that will NOT put single-quotes
on params that are integers or params that are specifiying OUT
arguments (i.e. the @ID should not be in quotes from what I'm reading
anyway..)

I'm new to working with MySQL Stored Procedures within PHP and would
like to have this generic function to make it easy to make calls to my
stored procedures from object's methods.

Such as:

private function insertTransaction($userid = null, $sessionid = null)
{
$params = array($userid, $sessionid, "@ID");
return callProcedure('insert_lmsTransaction',
$params);
}

Thanks for any inputs or help with heading me into the right
direction. I thought this would be simple, butt......

:)
 
E

Erwin Moller

phpCodeHead schreef:
I am trying to write a function that will allow me to call any MySQL
stored procedure and pass any array of values as parameters to pass to
that stored procedure.

So, far I have in my function:

$sql = "CALL $proc_name('" . implode("', '", $params) . "');";

Which is called like:

return callProcedure('someStoredProcedure', $params);

And is working fine, it generates an SQL statment like:

CALL someStoredProcedure('param1', 'param2', '@ID');

PROBLEM IS, it only works fine if all params are strings.

I am trying to come up with a "hack" that will NOT put single-quotes
on params that are integers or params that are specifiying OUT
arguments (i.e. the @ID should not be in quotes from what I'm reading
anyway..)

I'm new to working with MySQL Stored Procedures within PHP and would
like to have this generic function to make it easy to make calls to my
stored procedures from object's methods.

Such as:

private function insertTransaction($userid = null, $sessionid = null)
{
$params = array($userid, $sessionid, "@ID");
return callProcedure('insert_lmsTransaction',
$params);
}

Thanks for any inputs or help with heading me into the right
direction. I thought this would be simple, butt......

:)

Yeah, it's friday. :p
You might want to repost this to a mysql / PHP newsgroup. ;-)

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top