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

强颜欢笑 提交于 2019-12-02 11:27:29

问题


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.


回答1:


Untested and without proper error handling:

function oracleGetGata($query, $id="id") {
    global $conn;

    $results = array();
    $sql = OCI_Parse($conn, $query);
    OCI_Execute($sql);
    while ( false!==($row=oci_fetch_assoc($sql)) ) {
        $results[ $row[$id] ] = $row;
    }
    return $results;
}


来源:https://stackoverflow.com/questions/9803202/creating-a-function-to-grab-data-from-an-oracle-database-array-by-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!