Ajax event and sql error return by database

I am using sc_exec_sql in an ajax event in a form app.
Sometime the database returns an error as a result of a constrain violation and the user does not see the error message unless sql debug option is enabled.

In a production environment I cannot keep sql debug option enabled but I still want to see possible error returned by the database while executing an sc_exec_sql in an ajax event.

Any idea ?

look this

Thanks I have ready experimented with it but the problem is ajax not exec vs select.
In an ajax event automaric sql error handling does not work.

Hallo maxi,
Hallo Vincenzo,

I believe, that your problem is not the missing error out of sc_select within Ajax, but that the result will not be displayed.

Therefore lets expand the original function asdw_exec() a little bit into:

function asdw_exec_sql($befehl)
	{	// Improved sc_select with error handling
	
		sc_select(asdw_my_data,$befehl);
		if ({asdw_my_data} === false)
			{
				asdw_error({asdw_my_data_erro}. " aus SQL: " . $befehl);
			}
		else
			{
				return({asdw_my_data});
			}
	}

You will notice, that -in the case of an error- not die() is used, but another function named asdw_error().

The new function:

function asdw_error($t_message) 
	{ 
		// Error-handler

	if ($this->NM_ajax_flag == 1) 
		{
			$t_error_msg = '<font color="red"><br>FATAL ERROR :   ' . str_replace('<br>','',$t_message) 
				. '<br><br>' . date("Y-m-d H:i:s")
				. '<br>Project: ' . $this->Ini->nm_grupo
				. '<br>Application: ' . $this->Ini->nm_cod_apl
				. '</font>';
			sc_alert($t_error_msg);
			sc_error_message('asdw_error');
			sc_error_exit();
		}
	else
		{
			echo '<font color="red"><br>FATAL ERROR :   ' . str_replace('<br>','',$t_message) 
				. '<br><br>' . date("Y-m-d H:i:s")
				. '<br>Project: ' . $this->Ini->nm_grupo
				. '<br>Application: ' . $this->Ini->nm_cod_apl
				. '</font>'; 
			die();
		}

} 

In this function there is an if to check, if currently AJAX is in use or not.
If Ajax is used, the function uses sc_alert()... if not, it uses echo and die().

You can also see that the current project and the application where the error happened is shown.
Nice detail if you want to get a more detailed feedback from your users :wink:

Sample:
If your using the following code within a form in a button of type PHP or a button of type AJAX, you will get slightly different messages:

// Wrong SQL code to get an error !!
$my_sql = "UPDATE ge_test SET text_1=‘Hallo World.’ WHERE id=abc ";
$result = asdw_exec_sql($my_sql);

Using the PHP button:

Using the Ajax button:

Note:
There are plenty of these asdw_… functions building libraries for different use cases.
We use them when teaching Scriptcase and developing projects for our Scriptcase.coaching customers.

I hope this will help a little bit.

Sincerely
Gunter Eibl

2 Likes

thanks a lot Gunter, It works perfectly.