Reputation: 146500
I've learnt that odbc_execute()
does not always trigger a proper ODBC error when it returns FALSE
(not at least with the Oracle driver) and I can't fully trust odbc_error()
or odbc_errormsg()
. This situation is easy to detect when there wasn't a previous error because odbc_error()
returns an empty string. However, when it returns something I don't know whether it belongs to the last failed operation or it's a remain from a previous error.
The simplest solution would be to reset the odbc_error()
and odbc_errormsg()
functions when there's an error so next calls would start from scratch, but I couldn't find a supported way to do so. Can you figure out a way to do it?
Background: I'm enhancing a legacy app with a class that encapsulates database calls. That's why I need to make everything as generic as possible.
Upvotes: 11
Views: 2504
Reputation: 1
odbc_errormsg errors cannot be reset during script. So, actually an easy way to separate odbc_errormsg errors is to assign each obdc_connect an unique identifier. Examples show $db =@odbc_connect($somefile,......) but using either random or unique names => $db900 =@odbc_connect($somefile,......) or $myuniquename =@odbc_connect($somefile,......) will seperate error messages. Then using odbc_errormsg($myuniquename) will only return the error for that id.
Upvotes: 0
Reputation: 41
it's not necesary to reset the function I solved in this way:
function foo($sql){
$res = odbc_exec($this->dbconn, $sql);
if (odbc_error () and $res===false ) {
return $this->catchException(odbc_errormsg($this->dbconn));
}
return $res;
}
Upvotes: 3
Reputation: 462
odbc_error sometimes becomes confusing. executed sql string and error message may be different. In order to prevent this, we can hold all the executed sqls in an array, and after all execution finishes, we can check what the error messages are.
First let us define a executedSQL class which will hold the executed sqls info:
class executedSQL
{
public sql;
public result;
public error;
public message;
}
This class will hold all sql info and their result and returned messages.
If we use a class to connect the odbc db:
class myODBC
{
//holds the connection
public $connection;
//all executed sql string are added to this array as executedSQL object.
public $executedSQLs = array();
public function connect()
{
$this->connection = dbc_connect(" ", " ","") or die(odbc_errormsg());
}
public function execute($sql)
{
$execution = odbc_exec($this->connection, $sql); //execute it
//put all the results to executedSQL object
$executed = new executedSQL();
$executed->sql = $sql;
$executed->result = $execution;
$executed->error = odbc_error();
$executed->message = odbc_errormsg();
//push to executedSQLs var.
array_push($this->executedSQLs, $executed);
return $execution;
}
}
If we execute our sqls:
$db = new myODBC();
$db->connect();
$db->execute("select * from table1");
$db->execute("this is gonna be failed sql");
$db->execute("select * from table2");
print_r($db->executedSQLs);
This is going to print all sqls and their results. At this point we can see the executed sql and its related error message. So literally we are not resetting odbc_error but we make it more clear . If an error message is repeated twice, it is more propable that it belongs to previous executed sql. This way debugging becomes easier.
Upvotes: 1