How to call oracle r12 stored procedure with yii2 - php

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

Related

lumen + store procedure : not working bcz of multiple query in store procedure

my store procedure code is below,
BEGIN
SELECT * FROM email_template_punit_master WHERE punit_id = p_punit_id;
SELECT 'SUITE' AS array_name
FROM dp__view_punit_master as a
WHERE a.punit_id = p_punit_id;
END
my code in lumen to call this store procedure is below,
$result = DB::select('call '.env('DB_PREFIX').'mystoreprocedure('.$parameter.')');
it gives me result of only first query from store procedure not for second query,
if I change the order of query as below,
BEGIN
SELECT 'SUITE' AS array_name
FROM dp__view_punit_master as a
WHERE a.punit_id = p_punit_id;
SELECT * FROM email_template_punit_master WHERE punit_id = p_punit_id;
END
again it gives me result of first query instead of both the query result,
if I execute in mysql database it give me required result but call from lumen it gives first query result,
how to get all query result with lumen store procedure

MySQL Stored Procedure select not returning anything

I have the following stored procedure in a MySQL db. It doesn't return anything when I run it from my PHP code(Which I know is correct because it works for all of my other stored procedures). When I log into phpmyadmin and execute the stored procedure from the "Routines" page it works just fine. Any help would be much appreciated.
DELIMITER //
drop procedure if exists spGetTeam //
CREATE PROCEDURE spGetTeam(IN tid INT)
BEGIN
SELECT team_id, team_name FROM teams WHERE team_id = tid;
END //
DELIMITER ;
I'm calling the sp from my php like so:
$sql = 'CALL spGetTeam(2)';
The sp is called with the following:
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($result)) {
//handle return
}
This started working the next day. Not sure what happened. No code was changed. If I ever figure it out I will update the answer.

call an Oracle function that do an insert/update from PHP?

we have a function if we call it by using SELECT we get the error code ORA-14551 "Cannot preform a DML operation inside a query"
select pkg_tools.replace_site(1121,3343) from dual;
how to run this function and get the results
when we run it in SQL developer in this way:
declare
v_return VRACHAR2(200);
begin
v_return := pkg_tools.replace_site(1121,3343);
end;
works with no errors
and we need this function to be called inside PHP
note: I can not paste this function here, because it's to long, but it does allot of operations including insert and update!
A function that does DML cannot be called in a SELECT statement regardless of the calling language.
If you want to do DML and return a value, it would make much more sense to create a stored procedure with an OUT parameter rather than using a function. So it would make much more sense to
CREATE OR REPLACE PROCEDURE proc_name_return( p_1 IN NUMBER,
p_2 IN NUMBER,
p_ret OUT VARCHAR2 )
AS
BEGIN
p_ret := pkg_tools.replace.site( p_1, p_2 );
END;
and then call that stored procedure from PHP
$sql = 'BEGIN proc_name_return( :p_1, :p_2, :p_ret ); END;';
If you don't want to do that, my guess is that you could do something like this as well (adapted from one of the scripts on page 164 of the Underground PHP and Oracle Manual)
<?php
$c = oci_connect('hr', 'hrpwd', 'localhost/XE');
$s = oci_parse($c, "begin :ret :=pkg_tools.replace_site(1121,3343); end;");
oci_bind_by_name($s, ':ret', $r, 200);
oci_execute($s);
echo "Result is: ".$r;
?>

Calling MySQL stored procedure in PHP only updates one row

I have a stored procedure in MySQL that should update a column in a table. When I run
CALL recalculate_city_ids();
from a MySQL prompt, the correct number of rows are updated (a few hundred). When I run the command from PHP, only a single row is updated and I get no error.
Here's the PHP:
$con = mysqli_connect('localhost', 'user', 'pass', 'dbname' );
$result = $con->query( 'call recalculate_city_ids()' );
mysql_close($con);
And the SQL for the sproc:
DROP PROCEDURE IF EXISTS recalculate_city_ids;
DELIMITER $$
CREATE PROCEDURE recalculate_city_ids()
READS SQL DATA
BEGIN
DECLARE o_id INT DEFAULT 0;
DECLARE o_latitude FLOAT;
DECLARE o_longitude FLOAT;
DECLARE done INT DEFAULT 0;
DECLARE cur_users CURSOR FOR SELECT id, latitude, longitude FROM user WHERE latitude IS NOT NULL ORDER BY fname;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
OPEN cur_users;
users: LOOP
FETCH cur_users INTO o_id, o_latitude, o_longitude;
IF done=1 THEN
LEAVE users;
END IF;
SELECT #closest_city_distance:=fn_distance_cosine(o_latitude, o_longitude, latitude, longitude) AS distance, #closest_city_id:=id AS id FROM category WHERE zone="city" AND active=1 ORDER BY distance LIMIT 1;
UPDATE user SET city_id = IF(#closest_city_distance<=30, #closest_city_id, 0) WHERE id=o_id;
END LOOP users;
CLOSE cur_users;
END
$$
I can run other queries from PHP using mysqli (also tried the mysql object). I'm also unable to create stored procedures from PHP (no error) and had to do that part from a MySQL prompt as well.
PHP and my MySQL prompt are using the same username.
I think that PHP doesn't like it when a query returns multiple result sets. I don't really need to return anything since this is just a glorified UPDATE statement, so I changed my
SELECT #closest_city_distance:=fn_distance_cosine... query to
SELECT fn_distance_cosine(o_latitude, o_longitude, latitude, longitude) as distance,id into closest_city_distance, closest_city_id FROM category WHERE zone="city" AND active=1 ORDER BY distance LIMIT 1;
Since that was the only place a result set was getting returned, eliminating the returned sets fixed the problem.

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