PHP, Oracle and ODBC: How to check if result set is empty?

☆樱花仙子☆ 提交于 2019-12-13 18:07:44

问题


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

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