In order to not save a user that already exists in the stage, I am using the following IF NOT EXISTS statement in my query.
public function save() {
// new record
if ( $this->id == null ) {
$query = "IF NOT EXISTS(SELECT * FROM Table_A WHERE user_id ='{$this->user_id}' and stage_id = '{$this->stage_id}') INSERT INTO Table_A(".
'id, '.
'user_id, '.
'stage_id, '.
'create_date '.
') OUTPUT inserted.id '.
'VALUES ('.
'newid(), '.
'%s, '.
'%s, '.
'getdate()'.
')';
$rs = $this->db->Execute($query);
if ( $rs->EOF ) {
return false;
}
$rs->Close();
}
..It does the job, but when the user does exist it’s throwing this error:
PHP Fatal error: Uncaught exception ‘com_exception’ with message ‘Source: ADODB.Recordset
Description: Operation is not allowed when the object is closed.’
Wasn’t sure the best way to handle this was, any help is appreciated, thank you!
Because you close the Recordset by
$rs->Close();, the next data fetch from the Recordset is not allowed. You couldreturn $rs;, and after you get your data then close it.