Search code examples
phporacle-databaseoracle12coracle-call-interfacedatagrip

PHP/Oracle - OCI doesn't like my data type?


I'm new to OCI, but just trying to do a basic oci_bind_by_name in PHP to get it working. But even with a simple select statement and a dummy variable, it rejects the variable type.

Here's the code:

$conn = oci_connect($username, $password, $database);
$dummy = "dummy@dummy.com";
$u = oci_parse($conn, "select ca_email from pwv_google_group");
oci_bind_by_name($u, ':ca_email', $dummy);

But it just returns:

Warning: oci_bind_by_name(): ORA-01036: illegal variable name/number

Since this is a very simple query/parameter, I can only assume my syntax is wrong or something might be off on the Oracle side (ca_email really should be a varchar, but I'm using Datagrip which doesn't allow DESC command, so I don't know how to validate that). Is something else wrong?


Solution

  • oci_bind_by_name() is expecting you to bind a value for some form of input to the SQL statement - from the manual

    bv_name The colon-prefixed bind variable placeholder used in the statement. The colon is optional in bv_name. Oracle does not use question marks for placeholders.

    So for your example, it would be more like

    $u = oci_parse($conn, "select * from pwv_google_group where ca_email = :ca_email");
    oci_bind_by_name($u, ':ca_email', $dummy);
    

    As you are trying to retrieve the values from the data you just need to fetch the data as in (hacked from example #3 in manual)

    $u = oci_parse($conn, 'select ca_email from pwv_google_group');
    oci_execute($u);
    $row = oci_fetch_array($u, OCI_ASSOC+OCI_RETURN_NULLS);
    foreach ($row as $item) {
        print $item."<br>\n";
    }