问题
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