This question already has answers here:
How to call a MySQL stored procedure from within PHP code?
(4 answers)
Closed 9 years ago.
Can someone help me to create stored procedures in MySQL and call them from PHP?
You can create procedure very easily in MySQL --
Syntax :-
`//add a delimiter;
Create Procedure <procedure_name>(parameters "IN(for using inside the procedure)" and "OUT(for returning)")
Begin
// doing some thing.
end;
//end delimiter;`
Call Procedure - `CALL <procedure_name>(parameter1, parameter2, ....)`
Example -
`DELIMITER //
CREATE PROCEDURE EmployeeName(IN EId INT)
BEGIN
SELECT EmpName FROM Employee WHERE EmpId = EID;
END//`
Calling- assume i want to know the name of employee id "10", so --
CALL EmployeeName(10);
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_user_login` $$
CREATE DEFINER=`root`#`%` PROCEDURE `sp_user_login`(
IN loc_username VARCHAR(255),
IN loc_password VARCHAR(255)
)
BEGIN
SELECT user_id,
user_name,
user_emailid,
user_profileimage,
last_update
FROM tbl_user
WHERE user_name = loc_username
AND password = loc_password
AND status = 1;
END $$
DELIMITER ;
and call by,
mysql_connection specification and
$loginCheck="call sp_user_login('".$username."','".$password."');";
it will return the result from the procedure.
Related
DELIMITER $$
CREATE DEFINER= 'sports'`#`'%'` PROCEDURE `CheckMembership`(IN BrandId int,
IN Email varchar(128),
IN PhoneNumber varchar(10),
OUT ResponseCode int,
OUT ResponseMessage varchar(256))
BEGIN
declare _count int;
select count(*) from Member where Phone1 = PhoneNumber into _count;
set ResponseCode = 1;
set ResponseMessage = '';
END
Can anyone please help me to resolve this stored procedure as I a new to this and couldnot find any good solution in the internet . All I need to do is to get the count value by running it in php
For one thing, the syntax of the SELECT statement is wrong because the INTO clause is misplaced.
That should follow the SELECT list and come before the FROM keyword. For example:
SELECT COUNT(*) INTO _count
FROM Member
WHERE Phone1 = PhoneNumber ;
Syntax help is available in MySQL Reference Manual: http://dev.mysql.com/doc/refman/5.5/en/select-into.html
I'm not sure if this answers the question you were asking. (It's not at all clear what question you were asking.)
Q: I don't know how to call this stored procedure in a php file to get the answers.
From PHP, I think you would need to use MySQL user-defined variables for the OUT arguments, and retrieve those values with a separate SELECT statement:
As a simple demonstration of a pattern you could use, given the arguments of the procedure as shown in the question:
-- set user defined variables
SELECT #brandid = 'fee', #email = 'fi', #phonenumber = 'fo', #rc := '', #rm := '';
-- execute stored procedure with user-defined variables as arguments
CALL CheckMembership(#brandid, #email, #phonenumber, #rc, #rm);
-- retrieve user-defined variables
SELECT #rc AS ResponseCode, #rm AS ResponseMessage;
To return the "count" value, you could follow the same pattern. Modify the procedure to add another OUT parameter, and then set that to the value of that parameter in the procedure.
(This seems an unnecessary rigmarole, using a stored procedure to return a result which could just as easily be returned by a simple query.)
my Store procedure having 4 In type parameters.i want to use only 3 parameters when i call stored procedure in php mysql .
My stored Procedure is
DELIMITER $$
CREATE PROCEDURE `usptregister`
(
IN ID bigint,
IN name varchar(100),
IN email varchar(100),
IN phoneno varchar(15)
)
BEGIN
Insert INTO register
(id,fname, emailid, phone) values
(id,name , email ,phone) ;
END
and my php code where i am calling stored procedure passing only 3 parameters.
$call = mysql_query("CALL usptregister('0','".$_POST[name ]."','".$_POST[email ]."') ");
This code is working when i am passing same no of arguments when i am calling stored procedure.
I am new in stored procedure .so please help me.
Optional parameters are not supported by MySQL
I suggest you to pass the fourth parameter as null. Then in the procedure you can check if is null or not.
BEGIN
IF param IS NULL THEN
-- statements ;
ELSE
-- statements ;
END IF;
END$$
EDIT:
http://bugs.mysql.com/bug.php?id=15975
Although the request is 9 years old it's still not done.
I am trying to create an sql proceedure that will return the results back to the php page.
I want to be able to call the procedure as follows from the php
call procedure_name($var1)
which will run this script:
-- ---------------------------------------------------------------------------------
-- pUIGetCliStmtGenFlag
--
-- This procedure returns the status of the Trading Period:
--
-- ---------------------------------------------------------------------------------
drop procedure if exists pUIGetCliStmtGenFlag;
delimiter //
create procedure pUIGetCliStmtGenFlag(
IN pTradingPeriodMonth DATE
)
MODIFIES SQL DATA
COMMENT 'Checks if the TP has been closed'
begin
SELECT trading_period_month,
dt_end,
amt_traded_system_ccy
FROM ca_trading_period
WHERE trading_period_month=$var1
-- If amt_traded_system_ccy is NULL give the TP an open status otherwise mark as closed
IF amt_traded_system_ccy is NULL
$tpstatus='open'
ELSE
$tpstatus='closed'
end;
//
delimiter ;
I then want to be able to use $tpstatus in the rest of the php script.
I know this is simple but this is completely new to me and I cant find the correct method
You have to call the stored procedure from php and THEN get the values back, using OUT parameters of the procedure.
Please follow these instructions:
https://stackoverflow.com/a/48161/1291428
I'm trying to create an SQL procedure where is returns a specified value from the members table.
DELIMITER $$
DROP PROCEDURE IF EXISTS `qrgdev`.`ConfirmMember` $$
CREATE PROCEDURE `qrgdev`.`ConfirmMember` (
check_Username varchar(45),
check_Password varchar(255))
BEGIN
DECLARE intcheckId INTEGER(1);
DECLARE intAccessLevel INTEGER(1) DEFAULT 0;
SELECT id INTO intCheckId FROM members WHERE Username=Check_Username;
IF (intCheckId=0)
SELECT AccessLevel INTO intAccessLevel FROM members WHERE passwrd=check_Password;
ELSE
IF (intCheckId>0) THEN
Update members
Set LastSignIn=CURRENT_TIMESTAMP
Where ID=intCheckId;
SELECT AccessLevel INTO intAccessLevel FROM members WHERE id=intCheckId;
ELSE
Insert into members
(ID, Username, Administrator, Passwrd, LastSignIn)
values
(null, check_Username, 0, null, CURRENT_TIMESTAMP);
END IF;
END IF;
RETURN(intAccessLevel);
END $$
DELIMITER ;
so that i can have php set conditions based on that value
//...
$result = $mysqli->query("Call ConfirmMember($username,$password)");
//...
this started as a function before i learned that php cant call sql functions, its why it still hols return at the bottom and also get the point i want to return the value.
create an SQL procedure where is returns a specified value
There's your biggest problem. Procedures do not return values. Functions return values. OTOH you can pass references to variables as arguments in both procedures and functions - and the procedure/function can change the value.
this started as a function before i learned that php cant call sql functions
Who told you that? It's complete nonsense.
$result = $mysqli->query("Call ConfirmMember($username,$password)");
I assume you've escaped and quoted those variables.
Change PROCEDURE to FUNCTION then invoke it as....
SELECT ConfirmMember($username,$password)
Ok let me try this again.
query("CALL getemployee('$eml')");
$result = $sql->fetch_array();
?>
This is my stored procedure:
Delimiter //
Create procedure getemployee(in eml varchar(50))
Begin
Select * from employees where email = eml;
End//
Delimiter ;
The error i get from browser:
"Fatal error: Call to a member function fetch_array() on a non-object ...".
I use phpmyadmin version 3.2.4 and the mysql client version: 5.1.41
Your CREATE PROCEDURE statement appears to be invalid.
You need to give a name to your procedure, and to the parameter that you are passing. Therefore you may want to try something like the following example:
DELIMITER //
CREATE PROCEDURE procName (IN eml varchar(50))
BEGIN
SELECT * FROM employees WHERE email = eml;
END//
DELIMITER ;
The eml variable you use is not defined. Should not it be as following:
Create procedure getemployee (in eml varchar(50))
CREATE PROCEDURE get_customers(IN inCustomerIdList VARCHAR(100))
BEGIN
SELECT first_name, last_name, email FROM tbl_customers
WHERE customer_id IN (inCustomerIdList);
END$$