问题
Dear nerds and forumianers,
I have a delicious problem with PHP on a Oracle database with ODBC driver.
First the code:
$ora_conn = odbc_connect($ora_dsn, $ora_user, $ora_pass, SQL_CUR_USE_ODBC);
$ora_data = odbc_exec($ora_conn,$sql);
//echo "test: ".odbc_num_rows( $ora_data );
//exit;
if(odbc_num_rows( $ora_data ) > 0){
// do something in case no result came back
}
if($ora_data){
// do something in case result came back
}
If I uncomment the test, I always get back -1! No matter if the statement results in 0, 1 or 5 rows...
So with this code I never can get the information if there is no result, 1 result or more results coming back.
Do you have any idea on how to solve this issue?
Thank you very very much in advance for your help!
Best regards, Ingmar
回答1:
odbc_num_rows
seems to be reliable for INSERT, UPDATE, and DELETE queries only.
The manual says:
Using odbc_num_rows() to determine the number of rows available after a SELECT will return -1 with many drivers.
one way around this behaviour is to do a COUNT(*)
in SQL instead. See here for an example.
回答2:
if(odbc_num_rows( $ora_data ) == 0){
// do something in case no result came back
}
if(!$ora_data){
// do something in case result came back
}
回答3:
I solved it, thank you for your answers...
My solution is: Code first
$sql = "SELECT GUID FROM SMSITEACCESS WHERE PROJECTID = 901981 AND SID = " . $guid;
$ora_data = odbc_exec($ora_conn,$sql);
$arr = @odbc_fetch_array($ora_data);
if($arr['GUID'] == ""){
$dbh_insert = new PDO($dsn,$dbuser,$dbpass);
$dbh_insert->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql_insert = "INSERT INTO SMSITEACCESS (SID, CREATED, CREATEUSER, PROJECTID) VALUES ('".$guid."', SYSDATE, '".$userid."', '901981')";
$res_insert = $dbh_insert -> prepare($sql_insert);
$res_insert -> execute();
$sql_insert=null;
$res_insert=null;
$dbh_insert=null;
}
odbc_free_result($ora_data);
I added an include file firstly. This script checks one by one if every table to be worked with consists at last of one data set. If any of it doesn´t, it is created empty.
With this workaround I do not anymore have to check if data coming back or not...
It could have been much easier but ODBC-driver seams to be buggy in this con-sens.
Thnx for fast replies and have a good day.
Ingmar
来源:https://stackoverflow.com/questions/22295507/php-oracle-and-odbc-how-to-check-if-result-set-is-empty