Mysql stored procedure where clause - php

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

Related

How to call oracle r12 stored procedure with yii2

I have created a procedure in oracle R12, like following, but i cannot get the result in YII2.
CREATE OR REPLACE PROCEDURE myproctest(username IN OUT varchar2) is
encPass varchar2(255);
BEGIN
select
aa.ENCRYPTED_USER_PASSWORD into encPass from
fnd_user aa
where aa.USER_NAME = username;
username := encPass;
END;
It works fine when i execute it from PLSQL, but while executing it from yii2 it always shows 1. Following is the execution code:
$query=Yii::$app->dboracle->createCommand(
"declare
UserName varchar2(255) := 'MY-USERNAME';
begin
myproctest(UserName);
dbms_output.put_line(UserName);
end;");
$data = $query->execute();
I have also tried following for execution:
$query=Yii::$app->dboracle->createCommand("CALL myproctest('TEST-USER')");
$data = $query->execute();
But it shows following error:
ORA-06577: output parameter not a bind variable
I want to get the password field in a variable please help.
It seem you want to get value from oracle R12 procedure in PHP variable using YII2
Create procedure in Oracle R12 with output variable eg PWD in example
CREATE OR REPLACE PROCEDURE MYPROCTEST (USERNAME VARCHAR2,PWD OUT VARCHAR2) as
BEGIN
-- YOUR QUERY(S) OR assign password as per your requirement
PWD:='YOUR RESPONSE STRING';
end MYPROCTEST;
Calling procedure using YII2
$sql="
begin
MYPROCTEST(:IN_VAR_TESTUSER,:OUT_VAR_PASSWORD);
end;";
$query=Yii::$app->dbconnection->createCommand($sql,[':IN_VAR_TESTUSER'=>'TEST-USER']);
$query->bindParam(':OUT_VAR_PASSWORD', $RETURN_PASSWORD,\PDO::PARAM_STR|\PDO::PARAM_INPUT_OUTPUT,4000);
$query->execute();
You will get result in $RETURN_PASSWORD

Create a SQL FUNCTION with CodeIgniter

For a legacy project, I would like to execute this query using CodeIgniter :
DELIMITER $$
CREATE FUNCTION `getCustomerFullName`(intCustomerID INT)
RETURNS varchar(100) CHARSET latin1
return CONCAT(
(SELECT FirstName FROM Customer WHERE CustomerID = intCustomerID),
' ',
(SELECT LastName FROM Customer WHERE CustomerID = intCustomerID))$$
DELIMITER ;
When I try to use $this->db->query(), I get this error :
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 $$ CREATE FUNCTION `getCustomerFullName`(intCustomerID INT) RETURNS '
at line 1
How can I execute such "multi-line" query using CodeIgniter ?
The issue is in the query, not CodeIgniter. Use the regular delimiter inside your function:
DELIMITER $$
CREATE FUNCTION `getCustomerFullName`(intCustomerID INT)
RETURNS varchar(100) CHARSET latin1
return CONCAT(
(SELECT FirstName FROM Customer WHERE CustomerID = intCustomerID),
' ',
(SELECT LastName FROM Customer WHERE CustomerID = intCustomerID));
$$
DELIMITER ;
if you want to execute a Function or SP, you need to add this to mysql (in the mtop menu of your phpmyadmin or Workbench) you have many otions
in More (routine / new / add new)
create a function or sp
Then in CI you only call like
$this->db->query('call Function()');
Best solution i get was looking inside system libs and use it.
$this->db->simple_query("YOUR FULL SQL STRING");
I was looking on google and not solution provided.

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)

Categories