"$type [...] The type parameter tells Oracle which descriptor is used. Default to SQLT_CHR"
This is misleading. You don't need to specify a $type to insert numbers. Actually, there isn't any allowed $type constant to insert floating point numbers.
oci_bind_by_name
Description
bool oci_bind_by_name ( resource stmt, string ph_name, mixed &variable [, int maxlength [, int type]])oci_bind_by_name() binds the PHP variable variable to the Oracle placeholder ph_name. Whether it will be used for input or output will be determined at run-time and the necessary storage space will be allocated. The length parameter sets the maximum length for the bind. If you set length to -1 oci_bind_by_name() will use the current length of variable to set the maximum length.
If you need to bind an abstract datatype (LOB/ROWID/BFILE) you need to allocate it first using the oci_new_descriptor() function. The length is not used for abstract datatypes and should be set to -1. The type parameter tells Oracle which descriptor is used. Possible values are:
OCI_B_FILE - for BFILEs;
OCI_B_CFILE - for CFILEs;
OCI_B_CLOB - for CLOBs;
OCI_B_BLOB - for BLOBs;
OCI_B_ROWID - for ROWIDs;
OCI_B_NTY - for named datatypes;
OCI_B_CURSOR - for cursors, that were created before with oci_new_cursor().
Remember, that this function strips trailing whitespace. See the following example:
Örnek 2. oci_bind_by_name() example
|
Örnek 3. oci_bind_by_name() example
|
| Uyarı |
Do not use magic_quotes_gpc or addslashes() and oci_bind_by_name() simultaneously as no quoting is needed and any magically applied quotes will be written into your database as oci_bind_by_name() is not able to distinguish magically added quotings from those added intentionally. |
Returns TRUE on success or FALSE on failure.
Not: In PHP versions before 5.0.0 you must use ocibindbyname() instead. This name still can be used, it was left as alias of oci_bind_by_name() for downwards compatability. This, however, is deprecated and not recommended.
oci_bind_by_name
24-Sep-2008 05:14
09-May-2008 04:11
For IN string binds (i.e. INSERT or passing a value into a PL/SQL
procedure) where the length is not known when binding, use a length
that is longer than the longest possible string. If you don't know
what this is in advance, then re-call oci_bind_by_name() with the
actual size prior to each oci_execute() call.
For OUT binds, always specify a length so that PHP allocates enough
memory to hold the returned value.
The cardinal rule for binding is that the memory address used in the
bind call must continue to exist and be usable when the oci_execute
occurs. This is the common problem with 'foreach' loops.
09-May-2008 09:39
//Calling Oracle Stored Procedure
//I assume that you have a users table and three columns in users table i.e. id, user, email in oracle
// For example I made connection in constructor, you can modify as per your requirement.
//http://www.devshed.com/c/a/PHP/Understanding-Destructors-in-PHP-5/1/
<?php
class Users{
private $connection;
public function __construct()
{
$this->connection = oci_connect("scott", "tiger", $db); // Establishes a connection to the Oracle server;
}
public function selectUsers($start_index=1, $numbers_of_rows=20)
{
$sql ="BEGIN sp_users_select(:p_start_index, :p_numbers_of_rows, :p_cursor, :p_result); END;";
$stmt = oci_parse($this->connection, $sql);
//Bind in parameter
oci_bind_by_name($stmt, ':p_start_index', $start_index, 20);
oci_bind_by_name($stmt, ':p_numbers_of_rows', $numbers_of_rows, 20);
//Bind out parameter
oci_bind_by_name($stmt, ':p_result', $result, 20); // returns 0 if stored procedure succeessfully executed.
//Bind Cursor
$p_cursor = oci_new_cursor($this->connection);
oci_bind_by_name($stmt, ':p_cursor', $p_cursor, -1, OCI_B_CURSOR);
// Execute Statement
oci_execute($stmt);
oci_execute($p_cursor, OCI_DEFAULT);
oci_fetch_all($p_cursor, $cursor, null, null, OCI_FETCHSTATEMENT_BY_ROW);
echo $result;
echo '<br>';
var_dump($cursor); // $cursor is an associative array so we can use print_r() to print this data.
// you can return data from this function to use it at your user interface.
}
public function deleteUser($id)
{
$sql ="BEGIN sp_user_delete(:p_id, :p_result); END;";
$stmt = oci_parse($this->connection, $sql);
// bind in and out variables
oci_bind_by_name($stmt, ':p_id', $id, 20);
oci_bind_by_name($stmt, ':p_result', $result, 20);
//Execute the statement
$check = oci_execute($stmt);
if($check == true)
$commit = oci_commit($this->connection);
else
$commit = oci_rollback($this->connection);
return $result;
}
// You can make function for insert ,update using above two functions
}
?>
This is what the old OCI_B_* constants are now called:
(PHP 5.1.6 win32)
OCI_B_NTY - SQLT_NTY
OCI_B_BFILE - SQLT_BFILEE
OCI_B_CFILEE - SQLT_CFILEE
OCI_B_CLOB - SQLT_CLOB
OCI_B_BLOB - SQLT_BLOB
OCI_B_ROWID - SQLT_RDD
OCI_B_CURSOR - SQLT_RSET
OCI_B_BIN - SQLT_BIN
OCI_B_INT - SQLT_INT
OCI_B_NUM - SQLT_NUM
27-Jan-2007 04:27
Referes to:
Be careful that the variable argument is a reference. So, the following code does not work:
foreach($some_array as $key => $value)
{
OCIBindByName($stmt, $key, $value);
}
I assume this is because the contents of $value changes, even though the reference remains the same, so all bound variables end up pointing to the last loop iteration's value.
Instead use the following:
foreach($some_array as $key => $value)
{
OCIBindByName($stmt, $key, $some_array[$key]);
}
This dues to the foreach statement. $some_array in foreach() is a copy of the origine array. $key would be the "reference" in the copy, but $some_array[$key] points to the original one.
11-Jan-2007 08:48
This is an example of returning the primary key from an insert so that you can do inserts on other tables with foreign keys based on that value. The date is just used to provied semi-unique data to be inserted.
$conn = oci_connect("username", "password")
$stmt = oci_parse($conn, "INSERT INTO test (test_msg) values (:data) RETURN test_id INTO :RV");
$data = date("d-M-Y H:i:s");
oci_bind_by_name($stmt, ":RV", $rv, -1, SQLT_INT);
oci_bind_by_name($stmt, ":data", $data, 24);
oci_execute($stmt);
print $rv;
20-Feb-2006 04:48
Be careful that the variable argument is a reference. So, the following code does not work:
foreach($some_array as $key => $value)
{
OCIBindByName($stmt, $key, $value);
}
I assume this is because the contents of $value changes, even though the reference remains the same, so all bound variables end up pointing to the last loop iteration's value.
Instead use the following:
foreach($some_array as $key => $value)
{
OCIBindByName($stmt, $key, $some_array[$key]);
}
16-Feb-2006 12:43
If you do a wrapper for these functions there is a bug I found on php5.1.1 / oracle 9 / windows xp. see the section with oci_bind_by_name.
function db_layer_insert_1_row ($dbquery, $bindvar = array())
{
global $_db_layer_database;
global $_db_layer_lasterror;
if (false === ($stid = oci_parse($_db_layer_database, $dbquery))) {
db_layer_lasterror ();
return -1;
}
// Bind variables. NOTE substituting $bindvar[$bcol] with $bval causes
// all variables to be set to the last value of $bval?!?!
foreach ($bindvar as $bcol => $bval) {
oci_bind_by_name($stid, $bcol, $bindvar[$bcol]); //$bval); <- bug?!
}
...
16-Aug-2005 01:12
Note that there have been some changes on the constant identifiers and the documentation is currently not entirely accurate.
Running the following script;
<?php
foreach (array_keys(get_defined_constants()) as $const) {
if ( preg_match('/^OCI_B_/', $const) ) {
print "$const\n";
}
}
?>
Under PHP 4.4.0 I get;
OCI_B_SQLT_NTY < renamed to OCI_B_NTY with PHP5
OCI_B_BFILE
OCI_B_CFILEE
OCI_B_CLOB
OCI_B_BLOB
OCI_B_ROWID
OCI_B_CURSOR
OCI_B_BIN
Under PHP 5.0.4 I get;
OCI_B_NTY
OCI_B_BFILE < docs are wrong right now
OCI_B_CFILEE < docs are wrong right now
OCI_B_CLOB
OCI_B_BLOB
OCI_B_ROWID
OCI_B_CURSOR
OCI_B_BIN < it's a mystery
