FunkySayu
FunkySayu

Reputation: 8091

oci: Using an array instead of multiple oci_bind_by_name

I'm looking for a way to bind my prepared requests in oci like in PDO :

// Using PDO :
$stmt = $dbh->prepare($sql);
$stmt = $dbh->execute($params);

With for example

$sql = "SELECT * FROM `Users` WHERE `username` = ? AND `password` = ?";
$params = array('foo', 'toto');

My issue is that i want to use Oracle in one of my projects and i need to modify all my class DataBaseAccessor, which is based on the $sql and $params. If there's any way for me to not modify all my code, it could be cool :)

Upvotes: 0

Views: 1057

Answers (1)

timclutton
timclutton

Reputation: 13024

Use PDO_OCI. OCI8 functions only support named variable binding for reasons unknown to me.


Edit:

Here's an example:

$db = new PDO('oci:dbname=hostname/database;charset=AL32UTF8', 'usr', 'pwd');

$stmt = $db->prepare('select 1, 2, 3, ? as "test" from dual');
$stmt->execute(['x']);

var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));

Which gives the following result:

Array
(
    [0] => Array
        (
            [1] => 1
            [2] => 2
            [3] => 3
            [test] => x
        )
)

This assumes you want to access the fields by name (associative array) in which case it's important to add a field alias ("test" in the example above) or the field name will be pseudo-randomly assigned. For example:

[:PDO1] => 'x'

Upvotes: 1

Related Questions