1,105,636 Community Members

PHP MySQL 5.0 stored procedure

Member Avatar
rcasinillo_s
Light Poster
29 posts since Jan 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Any could give me a PHP script on how to call or execute mysql stored procedure.

Thank you.

Roland

Member Avatar
ameer
Newbie Poster
2 posts since Mar 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Before you installed the php_mysqli.dll

<?php


/* Connect to a MySQL server */
$link = mysqli_connect(
'localhost',  /* The host to connect to */
'root',       /* The user to connect as */
'root',   /* The password to use */
'db_name');     /* The default database to query */


if (!$link) {
printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
exit;
}



/* Send a query to the server */
if ($result = mysqli_query($link, "call se_proc('crm')")) {



/* Fetch the results of the query */
while( $row = mysqli_fetch_array($result) ){


echo ($row[0]. "--------- SR. " . $row[1] . "<br>");


}


/* Destroy the result set and free the memory used for it */
mysqli_free_result($result);
}


/* Close the connection */
mysqli_close($link);
?>
Member Avatar
kladizkov
Newbie Poster
2 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Oh it was simple, just execute "call proc_name"

http://fabinm.com/content/view/17/9/

Member Avatar
thinktejas
Newbie Poster
3 posts since Sep 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

There is an article on this site which shows how to access Mysql Stored procedures using PDO and PHP
http://tejuspratap.co.cc/2008/08/29/executing-mysql-stored-procedures-using-php-50/

Question Answered as of 5 Years Ago by ameer, kladizkov and thinktejas
Member Avatar
mazeroth
Light Poster
27 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 1 [?]
 
0
 

Here's a quick tutorial on executing mysql stored procedures in php using mysql, mysqli, and pdo for the people using the different database extension:

http://www.joeyrivera.com/2009/using-mysql-stored-procedures-with-php-mysqlmysqlipdo/

Member Avatar
thinktejas
Newbie Poster
3 posts since Sep 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

There is an article on this site which shows how to access Mysql Stored procedures using PDO and PHP
http://tejuspratap.co.cc/2008/08/29/executing-mysql-stored-procedures-using-php-50/

The article has been moved to http://www.tejuspratap.com/main/node/18

Member Avatar
thinktejas
Newbie Poster
3 posts since Sep 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

The article has been moved to http://www.tejuspratap.com/main/node/18

I am sorry I gave the wrong url before.

The article has been moved to http://www.tejuspratap.com/main/content/executing-mysql-stored-procedures-using-php-50

Member Avatar
kbramprasath
Newbie Poster
2 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
DROP PROCEDURE IF EXISTS `UsersR_DELETE_byPK` 
GO

CREATE PROCEDURE UsersR_DELETE_byPK
   (
        IN  UsersID             INT(11) 
)	
BEGIN 

   DELETE FROM UsersR   
                     
           WHERE  UsersID=UsersID;
                             
        
  END

it is my stored proceder how to call php code perfom delete operation

Member Avatar
topspindex
Newbie Poster
1 post since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Any could give me a PHP script on how to call or execute mysql stored procedure.

Thank you.

Roland

I'll give you a very simple connection.. and how to call a procedure very simply. here it is -
------->

//first create a connection to your database...
function connection(){
		$mysqli = new mysqli('$hostname','$dbusername', '$dbpassword', '$dbname');
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		
		return $mysqli;
		
	} 
//second create the function to call the stored procedure
  function query($query){
//call the connection function
  if($result = connection()->query($query)){
			while($row = $result->fetch_assoc()){
					$data[] = $row;
			}
			return $data;
			}else {
			print('error on:'.$query.mysqli_error($this));
                      }
 }
//then call the function to execute stored procedure
query("call store_proc('$param1')");

done! it's that simple... thanks

Member Avatar
vsmash
Junior Poster
138 posts since Feb 2010
Reputation Points: -1 [?]
Q&As Helped to Solve: 18 [?]
Skill Endorsements: 0 [?]
 
0
 

THat was an excellent reply.

Member Avatar
centhi
Newbie Poster
2 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Problem when

$param1 = "Sky's Blue";
query("call store_proc('$param1')");

I'll give you a very simple connection.. and how to call a procedure very simply. here it is -
------->

//first create a connection to your database...
function connection(){
		$mysqli = new mysqli('$hostname','$dbusername', '$dbpassword', '$dbname');
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		
		return $mysqli;
		
	} 
//second create the function to call the stored procedure
  function query($query){
//call the connection function
  if($result = connection()->query($query)){
			while($row = $result->fetch_assoc()){
					$data[] = $row;
			}
			return $data;
			}else {
			print('error on:'.$query.mysqli_error($this));
                      }
 }
//then call the function to execute stored procedure
query("call store_proc('$param1')");

done! it's that simple... thanks

Member Avatar
centhi
Newbie Poster
2 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi !

Have a problem when parameter with single quote.

$param1 = "Test's Postt's";
query("call store_proc('$param1')");

Please help me to fix this.

Member Avatar
mazeroth
Light Poster
27 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 1 [?]
 
0
 

You have to escape special characters. One way to do this is to user addslashes for example:

query("call store_proc('addslashes($param1)')");

If you are using mysql, you can use their mysql_real_escape_string instead:

http://us2.php.net/manual/en/function.mysql-real-escape-string.php

Member Avatar
alexopoulos7
Newbie Poster
1 post since Jul 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article