tags:

views:

29

answers:

3

I have a form which contains a lot of elements, my DB guy has generated an SP and now i need to call that stored procedure. My form has twenty elements, when i click on submit the data must be saved in database. I know how to do this on insert query, but how to call an SP and perform this operation.

There are 10 tables where the data is going to get saved.

+4  A: 

I've never used stored procedures in MySQL, but it's no different from "plain" SQL queries, except you use CALL as the query keyword.

Instead of:

INSERT INTO table (column1, column2, column3) VALUES ('value 1', 'value 2', 3)

you do:

CALL function_name('value 1', 'value 2', 3);

(this is assuming your stored procedure is called function_name and takes 2 string arguments and a numeric argument.

Andy Shellam
And of course you can do it using prepared statements as well ie. use CALL function_name(:param1,:param2,:param3) and set the value for the parameters afterwards in your code (for input/output parameters check http://php.net/manual/en/pdo.prepared-statements.php).
wimvds
@wimvds - good point.
Andy Shellam
Thanks Andy for your reply
+1  A: 

PDO has a nice way of working with stored procedures through prepared statements. I strongly recommend working with it instead of working with "plain damn SQL".

<?php
// Calling a stored procedure with an output parameter
$stmt = $dbh->prepare("CALL sp_returns_string(?)");
$stmt->bindParam(1, $return_value, PDO::PARAM_STR, 4000); 

// call the stored procedure
$stmt->execute();

print "procedure returned $return_value\n";
?>
Elzo Valugi
Thanks, i feel this is slightly complex.
Any reasons to for PDO instead of plain SQL apart from security
PDO is still using plain SQL - it's exactly the same SQL as you'd pass to mysql_query, for example. The difference is it's a better coding methodology (it's object-oriented.) From a security point-of-view it allows you to do parameterised queries so you don't have to worry about escaping all your input, but you can do that with the normal MySQL extension anyway, I believe.
Andy Shellam
security is a sufficient enough argument for me.
Elzo Valugi