How to write a mysql procedure to insert data? - php

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.

Related

how to call stored procedure with optional arguments

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.

sql procedure import error

DELIMITER $$
drop procedure IF EXISTS `simpleproc`$$
CREATE PROCEDURE `simpleproc`(OUT param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END $$
DELIMITER ;
I am trying to import the above code using PHP but I'm getting this error:
Error performing query '':You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$ drop procedure IF EXISTS simpleproc$$ CREATE PROCEDURE `simplepro' at line 1
DELIMITER $$
DROP PROCEDURE IF EXISTS `simpleproc`$$
CREATE PROCEDURE `simpleproc`(OUT param1 INT)
READS SQL DATA
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END $$
DELIMITER ;$$
The above syntax can only be executable at MySQL command line client.
The same won't work using server side scripting language.
Instead you can use the following as a string to construct the query and pass to mysqli->query
CREATE PROCEDURE simpleproc (OUT param1 INT )
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END;
Note:
If you have only a single statement to be processed and executed in a stored procedure,
you don't even need begin - end blocks.
This is correct:
CREATE PROCEDURE simpleproc (OUT param1 INT )
SELECT COUNT(*) INTO param1 FROM t;
You have an error in your procedure syntax, please use following
DELIMITER $$;
DROP PROCEDURE IF EXISTS `simpleproc`$$
CREATE PROCEDURE `simpleproc` (OUT param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END$$
DELIMITER ;$$
This will solve your problem

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.

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)

Mysql stored procedure where clause

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$$

Categories