how to call stored procedure with optional arguments - php

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.

Related

How to write a mysql procedure to insert data?

CREATE PROCEDURE dasd (
IN aSupplierID varchar(4),
IN aSupplierName varchar(100),
IN aSupplierAddress varchar(255),
IN aSupplierTelPhone varchar(10))
BEGIN
insert
into
supplier
(SupplierID,SupplierName,SupplierAddress,SupplierTelPhone)
values
(aSupplierID,aSupplierName,aSupplierAddress,aSupplierTelPhone);
END
I find an error in the line 'values(aSupplierID,aSupplierName,aSupplierAddress,aSupplierTelPhone);
END'.
Please can you try following one
CREATE PROCEDURE dasd (
IN aSupplierID varchar(4),
IN aSupplierName varchar(100),
IN aSupplierAddress varchar(255),
IN aSupplierTelPhone varchar(10))
BEGIN
insert
into
supplier
(SupplierID,SupplierName,SupplierAddress,SupplierTelPhone)
values
(in_aSupplierID,in_aSupplierName,in_aSupplierAddress,in_aSupplierTelPhone);
END
Try this
DELIMITER $$
CREATE PROCEDURE das1d (
IN aSupplierID VARCHAR(4),
IN aSupplierName VARCHAR(100),
IN aSupplierAddress VARCHAR(255),
IN aSupplierTelPhone VARCHAR(10))
BEGIN
INSERT
INTO
supplier
(SupplierID,SupplierName,SupplierAddress,SupplierTelPhone)
VALUES
(aSupplierID,aSupplierName,aSupplierAddress,aSupplierTelPhone);
END$$
DELIMITER ;
The delimiter is changed to $$ to enable the entire definition to be passed to the server as a single statement, and then restored to ; before invoking the procedure. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself. Click here for more details about defining stored procs.

Mysql Stored Procedure

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.)

MySQL stored procedures in PHP with examples [duplicate]

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.

Passing variables from SQL proceedure to PHP

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

Trying to Call MYSQL Procedure to return int value based on Condition to php

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)

Categories