PHP warning and error page

Any critical bugs will be fixed within 24-48 hours.
Post Reply
suman4u
Posts: 8
Joined: 06 Feb 2023, 09:00
Name: Suman Halder
Location: Kolkata

PHP warning and error page

Post by suman4u »

After update to 3.5 and changing PHP to 8.1 the following error code occurs when form is submitted:
Warning: Trying to access array offset on value of type null in .../includes/classes/fieldstypes/fieldtype_php_code.php(175) : eval()'d code on line 6
Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in .../includes/classes/fieldstypes/fieldtype_php_code.php(175) : eval()'d code on line 7
Warning: Cannot modify header information - headers already sent by (output started at .../includes/classes/fieldstypes/fieldtype_php_code.php(175) : eval()'d code:6) in .../includes/functions/urls.php on line 26
User avatar
support
Site Admin
Posts: 6231
Joined: 19 Oct 2014, 18:22
Name: Sergey Kharchishin
Location: Russia, Evpatoriya

Re: PHP warning and error page

Post by support »

You have to update your php code. PHP 8.1 not support null value to strlen function.
suman4u
Posts: 8
Joined: 06 Feb 2023, 09:00
Name: Suman Halder
Location: Kolkata

Re: PHP warning and error page

Post by suman4u »

This was my PHP code. Can you suggest strlen alternative?
//
$status = [157];
$case_id = [id];
//$output_value = $case_id;
$ts_query = db_query("SELECT app_comments.date_added AS ts FROM app_comments INNER JOIN app_comments_history ON app_comments_history.comments_id=app_comments.id WHERE app_comments_history.fields_value=43 AND app_comments.items_id= '" . $case_id ."'");
$ts_info = db_fetch_array($ts_query);
$ts = $ts_info['ts'];
if(strlen($ts) > 0){
$output_value = date('d/m/Y H:i',$ts);
}else{
$output_value = "";
}
eddydeniro
Posts: 174
Joined: 23 Feb 2021, 16:31
Name: Edi Supriyadi
Location: BDG Indonesia

Re: PHP warning and error page

Post by eddydeniro »

You have to consider the possibility of empty result of your query. Because if it is, it will give null value to $ts which caused the warning.
Try changing your code like this:

Code: Select all

$status = [157];
$case_id = [id];
//$output_value = $case_id;
$ts_query = db_query("SELECT app_comments.date_added AS ts FROM app_comments INNER JOIN app_comments_history ON app_comments_history.comments_id=app_comments.id WHERE app_comments_history.fields_value=43 AND app_comments.items_id= '" . $case_id ."'");
if(db_num_rows($ts_query))
{
	$ts_info = db_fetch_array($ts_query);
	$ts = $ts_info['ts'];
	if(strlen($ts) > 0){
		$output_value = date('d/m/Y H:i',$ts);
	}else{
		$output_value = "";
	}
}
suman4u
Posts: 8
Joined: 06 Feb 2023, 09:00
Name: Suman Halder
Location: Kolkata

Re: PHP warning and error page

Post by suman4u »

eddydeniro wrote: 03 Feb 2024, 19:25 You have to consider the possibility of empty result of your query. Because if it is, it will give null value to $ts which caused the warning.
Try changing your code like this:

Code: Select all

$status = [157];
$case_id = [id];
//$output_value = $case_id;
$ts_query = db_query("SELECT app_comments.date_added AS ts FROM app_comments INNER JOIN app_comments_history ON app_comments_history.comments_id=app_comments.id WHERE app_comments_history.fields_value=43 AND app_comments.items_id= '" . $case_id ."'");
if(db_num_rows($ts_query))
{
	$ts_info = db_fetch_array($ts_query);
	$ts = $ts_info['ts'];
	if(strlen($ts) > 0){
		$output_value = date('d/m/Y H:i',$ts);
	}else{
		$output_value = "";
	}
}
My code was working fine with PHP 7.4 but as strlen() got deprecated in PHP 8.1, so I need a workaround to replace strlen()
Can you please help?
User avatar
support
Site Admin
Posts: 6231
Joined: 19 Oct 2014, 18:22
Name: Sergey Kharchishin
Location: Russia, Evpatoriya

Re: PHP warning and error page

Post by support »

You have to check if $ts_info exist before strlen
suman4u
Posts: 8
Joined: 06 Feb 2023, 09:00
Name: Suman Halder
Location: Kolkata

Re: PHP warning and error page

Post by suman4u »

Thanks @Sergey and @Edi. It's working
Post Reply