创建一个从Oracle数据库中获取数据的函数(按ID排列的数组)

时间:2012-03-21 10:58:30

标签: php oracle select oci

我正在尝试创建一个只允许我将SQL语句传递给它的函数,它将根据我传递的唯一ID生成一个数组:

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;
}

例如,我希望此查询$array = oracleGetData('select * from table')返回类似的内容:

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

而不是它现在正在回归的东西:

[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
)

我真的很感激任何帮助,因为该功能可以节省我很多的时间!谢谢。

1 个答案:

答案 0 :(得分:2)

未经测试且没有正确的错误处理:

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;
}