Creating a function to grab data from an Oracle database (array by ID)

Posted by Nick on Stack Overflow See other posts from Stack Overflow or by Nick
Published on 2012-03-21T10:58:29Z Indexed on 2012/03/21 11:29 UTC
Read the original article Hit count: 145

Filed under:
|
|
|

I'm trying to create a function that will simply allow me to pass an SQL statement into it, and it will generate an array based on a unique ID I pass it:

function oracleGetGata($query, $id="id") {
    global $conn;
    $sql = OCI_Parse($conn, $query);
    OCI_Execute($sql);
    OCI_Fetch_All($sql, $results, null, null, OCI_FETCHSTATEMENT_BY_ROW);
    return $results;
}

 

For example I'd like this query $array = oracleGetData('select * from table') to return something like:

[1] => Array
(
    [Title] => Title 1
    [Description] => Description 1
)
[2] => Array
(
    [Title] => Title 2
    [Description] => Description 2
)
[3] => Array
(
    [Title] => Title 3
    [Description] => Description 3
)

 

Rather than what it's returning at the moment:

[0] => Array
(
    [ID] => 3
    [TITLE] => Title 3
    [DESCRIPTION] => Description 3
)
[1] => Array
(
    [ID] => 1
    [TITLE] => Title 1
    [DESCRIPTION] => Description 1
)
[2] => Array
(
    [ID] => 2
    [TITLE] => Title 2
    [DESCRIPTION] => Description 2
)

 

I'd really appreciate any help with this, as the function would save me lots of time! Thank you.

© Stack Overflow or respective owner

Related posts about php

Related posts about Oracle