phporacle-databaseoci8

PHP oci_execute wont return anything, when it should


I'm trying to implement a find by criteria method with OCI8 in PHP, where if the object has a parameter set, it should query all records, where the given parameters are like the ones in the record. But for some reason the query result is empty.

$id = $model->getId();

// create the criterias for each parameters
if (isset($id)) $crits[] = "ID = :id";
// ...

if (!empty($crits))
    //extend the sql with the criterias
    $sql .= " AND " . implode(" AND ", $crits);

    if (!$stmt = oci_parse($this->dataSource->getConnection(), $sql))
       throw new DataAccessException('parse ' . json_encode(oci_error($stmt)));
 
    // bind the required parameters
    if (isset($id) && !oci_bind_by_name($stmt, ':id', $id, -1))
       throw new DataAccessException('bind id ' . json_encode(oci_error($stmt)));
    // ...
    
    if (!oci_execute($stmt, OCI_DEFAULT))
       throw new DataAccessException('exec ' . json_encode(oci_error($stmt)));

    // handle result...
}

The sql must be correct, I copied the one assembled by the code, I did the binding manually, and got the correct results, so I think the problem should be in the binding.

I checked if the error handling is not correct, and the bindig does not actually happen, by removing them, but that cannot be the case, since then the oci_execute will throw an error.

Since checking the binded sql is not possible, I'm stuck.

Thanks for your help!


Solution

  • I've found the issue, the value of $id is stored as string, but it was numeric, so I had to modify the binding like this:

    oci_bind_by_name($stmt, ':id', $id, -1, is_numeric($id) ? SQLT_INT : SQLT_CHR);