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.
"; $patron_error="/SQL\d+/"; if(preg_match($patron_error, $odbc_errormsg, $coincidencias)){ throw new Exception($coincidencias[0].";".$odbc_errormsg); } } For now, it's only useful with error messages of DB2. I hope find a way to manage errors of another databases with the php odbc driver – vteran93 Dec 17 '14 at 21:41