Basic Stored Procedures


I am learning about stored routines and am having a hard time getting results.  I have this in mysql:

CREATE DEFINER=`user`@`localhost` PROCEDURE `test`()
SELECT * FROM `database`.`tablecds`;

Open in new window

How would I call on the results?  I've tried the following and can't get an answer, so obviously I am doing it wrong.
$sql = "CALL test();" or die(mysql_error());

mysql_query($sql, $con);

$sql = "SELECT @cd;";

$result = mysql_query($sql, $con);

$row = mysql_fetch_assoc($result);

$echo= $row['cd'];

Open in new window

Please enlighten me!  Thank you in advance for any help!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ahmed MerghaniSoftware EngineerCommented:
Hi happysunny,

If you post the exact result or error, that will be helpful to solve this issue.

Anyway, this link very useful and show different way to call stored procedure from PHP:
nemws1Database AdministratorCommented:
You need to use the "mysqli" (note the "i" on the end) interface with PHP in order to CALL() a MySQL function and get the results.

The reason for this is that the usual 'mysql' interface can't handle multiple sets of results.  Imagine if you will your stored procedure with *two* SELECT statements on different table.  How do you get both sets of results back?  The usual PHP mysql interface can't (easily) do that, but mysqli can.

Have fun with stored procedures!  They are awesome and let you do some really cool things!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
happysunnyAuthor Commented:
I see...Does mysqli have to be configured on my VPS?  Can you give me an idea on how to go about that?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!


$rs = $mysqli->query( ‘CALL test()’ );
while($row = $rs->fetch_object())

 ... do something here ...

Ahmed MerghaniSoftware EngineerCommented:
You can use store procedure with MySQL, MySQLi and ADO.

More information can be found in this link:
happysunnyAuthor Commented:
Yes, if I can stick with mysql instead of mysqli, I'd like to do that.  But is it possible?

I tried to do this from the link...

$rs = mysql_query( 'CALL test()' );
while($row = mysql_fetch_assoc($rs))

Open in new window

but I got the following error:
"Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource"

Should I just make the switch to mysqli or can I stay with mysql?
nemws1Database AdministratorCommented:
You can only use the 'mysql' interface if your procedures don't return any rows (you can use them if they return a set of values and use MySQL variables to store those values).

It is much better/easier/more correct to use the 'mysqli' PHP interface when using complex stored procedures.
>>Should I just make the switch to mysqli or can I stay with mysql?<<

switch to mysqli.
happysunnyAuthor Commented:
Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.