• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

Basic Stored Procedures

Hello,

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

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

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!
0
happysunny
Asked:
happysunny
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
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:

http://www.joeyrivera.com/2009/using-mysql-stored-procedures-with-php-mysqlmysqlipdo/
0
 
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!
0
 
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?
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
ralmadaCommented:
try

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

 ... do something here ...

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

More information can be found in this link:

http://www.joeyrivera.com/2009/using-mysql-stored-procedures-with-php-mysqlmysqlipdo/
0
 
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))
{
debug($row);
}

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?
0
 
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.
0
 
ralmadaCommented:
>>Should I just make the switch to mysqli or can I stay with mysql?<<

switch to mysqli.
0
 
happysunnyAuthor Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now