Binds a PHP variable to an Oracle parameter
(PHP 4, PHP 5 <= 5.0.5)
Example 1679. ora_bind() example
Code Examples / Notes » ora_bind
The Oracle parser doesn't like DOS line endings (cr/lf). Save as Unix file (lf line ending only).
ora_bind looks for the bind variables globally. If your are using ora_bind
from within a class you'll have to delcare the bind variables globally and reference them using eg. $GLOBAL["myVar"].
ahmed dot adaileh
It was hard a little bit to understand the example above. Since it did not work properly :(
I managed to get a similar one running, the whole example is as following:
$connection = ora_logon("user@DBname", "password");
$cursor = ora_open($connection);
$cursor=ora_parse($cursor, "begin user.user_create (:name,:surname,:street,:country,:postal,:city,:tel,:mail,:number); end;");
$name = 'a';
$surname = 'a';
$street = 'a';
$country = 'a';
$postal = 'a';
$city = 'a';
$tel = 'a';
$mail = 'a';
ora_bind($cursor, "name", ":name", 32, 1);
ora_bind($cursor, "surname", ":surname", 32, 1);
ora_bind($cursor, "street", ":street", 32, 1);
ora_bind($cursor, "country", ":country", 32, 1);
ora_bind($cursor, "postal", ":postal", 32, 1);
ora_bind($cursor, "city", ":city", 32, 1);
ora_bind($cursor, "tel", ":tel", 32, 1);
ora_bind($cursor, "mail", ":mail", 32, 1);
ora_bind($cursor, "number", ":number", 32, 2);
// The following step is to charge the output variable with an intial value (Actually still doesn't know why? but it works like this ;-)) )
$okdnr = "a";
print "okdnr is $okdnr";
The called stored procedure is as following:
CREATE OR REPLACE procedure customer_create
iname in varchar2,
insurname in varchar2,
instreet in varchar2,
inpostal in varchar2,
incity in varchar2,
intel in varchar2,
inemail in varchar2,
okdnr out varchar2)
..... SOME CODE ....
The most important part of all this is the ora_bind function. Becarefull while writing the syntax regarding the definition wether the variable is an INPUT (1) variable or an OUTPUT variable (2).
Hope this help someone!!
Inputting integers using ORA_BIND.
There is a potential problem in binding oracle integers using php. PHP supports only 32 bit integers where as oracle supports 128. so when you are trying to bind an 128 bit oracle integer, php only interprets the first 32 bits. This results in undesired behaviour. The easiest way around it is not to use bind operations and instead do the whole conversion during the parsing phase. This means you have to parse for each input. A more optimal solution would be to change the oracle inputs to be varchar2 and let oracle deal with the type conversions.
Ahmed's solution is based on the sample given to him in http://forums.oracle.com/forums/thread.jsp?forum=178&thread=206340
Here's a similar example answering the question
"How do I get the return value from a PL/SQL function?"
Sample using "oracle" driver to get the value of a PL/SQL function.
Before running this script create this function using SQL*Plus:
create or replace function myfunc(myparam IN varchar2) return varchar2 as
Tested with PHP 4.3.3 against Oracle 9.2
$my_db_conn = ora_logon("scott@mydb", "tiger");
$my_cursor = ora_open($my_db_conn);
$sql = "begin :mybindvar := myfunc('abcde'); end;";
ora_parse($my_cursor, $sql, 0);
$r = ora_bind($my_cursor, "mybindvar", ":mybindvar", 5, 1);
// Set the OUT bind variable to anything (?), else get:
// Warning: Can't find variable for parameter
// Notice: Undefined variable: mybindvar
$mybindvar = NULL;
print 'Return value is: '. $mybindvar;