Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • 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 MerghaniCommented:
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
 
nemws1Commented:
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
ralmadaCommented:
try

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

 ... do something here ...

}
0
 
ralmadaCommented:
0
 
Ahmed MerghaniCommented:
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
 
nemws1Commented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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