Jump to content

PHP8 PHP Fatal error: Uncaught TypeError: Unsupported operand types: string - int


ohno

Recommended Posts

I'm getting this error for a blog script that is on my site :-

PHP Fatal error:  Uncaught TypeError: Unsupported operand types: string - int

 

The line in questions is this :-

$prev = $page - 1;

If I comment out this block of code the blog appears (with other errors, but I'll move on to those if I can fix this first!).

/* Setup page vars for display. */
    if (isset($page) and $page == 0) $page = 1;                    //if no page var is given, default to 1.
    $prev = $page - 1;                            //previous page is page - 1
    $next = $page + 1;                            //next page is page + 1
    $lastpage = ceil($total_pages/$limit);        //lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;                        //last page minus 1

 

Any idea's on what's needed to fix this script? I've contacted the original author but they haven't got back to me, I guess PHP8 maybe a little too new for them.

Thanks for any help....

 

Link to comment
Share on other sites

That error very specifically means the thing on the left side of the minus is a string, and what's more, that the contents of the string aren't numeric.

Are you sure $page is an int when that error happens? Could it be that you're getting the value from $_GET and the URL triggering the error has an invalid ?page= value?

Link to comment
Share on other sites

Well, I can only dump that variable when running PHP7.4 as if that code is left in place (the code I commented out as above) with PHP8 I get nothing! At the moment PHP8 is looking like a stack of work as a lot of things have broken when testing with 8! Even LiveZilla which we use for online chat (which sadly closed last year) breaks when switching to 8. Beginning to think it's not worth the hassle but 7.4 will only have security updates for another 18 months or so.

Link to comment
Share on other sites

When I say "nothing" I just get this error on the page :-

Fatal error: Uncaught TypeError: Unsupported operand types: string - int in /home/public_html/blog/blog.php:614 Stack trace: #0 {main} thrown in /home/public_html/blog/blog.php on line 614

Line 614 :-

$prev = $page - 1;

 

The whole block of code is this :-

// pagination query and variables starts
	$sql = "SELECT count(*) as total FROM ".$TABLE["Posts"]." WHERE status<>'Hidden' " .$search;
	$sql_result = sql_result($sql);
	$row = mysqli_fetch_array($sql_result);
	mysqli_free_result($sql_result);
	$total_pages = $row["total"];
	$adjacents = 2; // the adjacents to the current page digid when some pages are hidden
	$limit = $Options["per_page"];  //how many items to show per page
	
	
	$page = '';
	if(isset($_GET["p"]) and $_GET["p"]!='') {
		$page = (int) abs(SafetyDB(urldecode($_GET["p"])));
	}
	
	if(isset($page) and $page!='') { 
		$start = ($page - 1) * $limit;  //first item to display on this page
	} else {
		$start = 0;	 //if no page var is given, set start to 0
	}
	
	/* Setup page vars for display. */
	if (isset($page) and $page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//previous page is page - 1
	$next = $page + 1;							//next page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	// pagination query and variables ends

 

 

 

Link to comment
Share on other sites

I now get this error (in PHP8)

Could not execute MySQL query: SELECT * FROM blog_posts WHERE status<>'Hidden' ORDER BY publish_date DESC LIMIT -1,1 . Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '-1,1' at line 4

&&

[16-Feb-2021 19:49:52 Europe/London] PHP Warning:  Undefined variable $search in /home/public_html/blog/meta.php on line 50

Line 50 of meta.php :-

WHERE status<>'Hidden' " .$search . "

Which is in this block of code :-

$sql = "SELECT * FROM ".$TABLE["Posts"]." 
			WHERE status<>'Hidden' " .$search . "  
			ORDER BY publish_date DESC 
			LIMIT " . ($pageNum-1)*$Options["per_page"] . "," . $Options["per_page"];	
	$sql_result = sql_result($sql);

 

Link to comment
Share on other sites

  • 1 year later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.