ohno Posted February 16, 2021 Share Posted February 16, 2021 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.... Quote Link to comment https://forums.phpfreaks.com/topic/312161-php8-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-int/ Share on other sites More sharing options...
Barand Posted February 16, 2021 Share Posted February 16, 2021 What does var_dump($page) say it is? Quote Link to comment https://forums.phpfreaks.com/topic/312161-php8-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-int/#findComment-1584513 Share on other sites More sharing options...
ohno Posted February 16, 2021 Author Share Posted February 16, 2021 int(1) Quote Link to comment https://forums.phpfreaks.com/topic/312161-php8-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-int/#findComment-1584515 Share on other sites More sharing options...
Barand Posted February 16, 2021 Share Posted February 16, 2021 My theory bites the dust then. I thought it might be a string. Quote Link to comment https://forums.phpfreaks.com/topic/312161-php8-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-int/#findComment-1584516 Share on other sites More sharing options...
requinix Posted February 16, 2021 Share Posted February 16, 2021 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? Quote Link to comment https://forums.phpfreaks.com/topic/312161-php8-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-int/#findComment-1584517 Share on other sites More sharing options...
ohno Posted February 16, 2021 Author Share Posted February 16, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312161-php8-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-int/#findComment-1584525 Share on other sites More sharing options...
requinix Posted February 16, 2021 Share Posted February 16, 2021 36 minutes ago, ohno said: with PHP8 I get nothing! Well there ya go: an empty string is not a valid number. You didn't post the code that tries to assign $page so Quote Link to comment https://forums.phpfreaks.com/topic/312161-php8-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-int/#findComment-1584528 Share on other sites More sharing options...
ohno Posted February 16, 2021 Author Share Posted February 16, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/312161-php8-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-int/#findComment-1584534 Share on other sites More sharing options...
Barand Posted February 16, 2021 Share Posted February 16, 2021 On line 601 you set $page to "" (empty string) You only set it to an int value if $_GET['page'] is set. (603) Change 601 to $page = 0; Quote Link to comment https://forums.phpfreaks.com/topic/312161-php8-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-int/#findComment-1584537 Share on other sites More sharing options...
ohno Posted February 16, 2021 Author Share Posted February 16, 2021 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); Quote Link to comment https://forums.phpfreaks.com/topic/312161-php8-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-int/#findComment-1584540 Share on other sites More sharing options...
Barand Posted February 16, 2021 Share Posted February 16, 2021 It doesn't like anything less than 0 as an offset value in the LIMIT clause. Quote Link to comment https://forums.phpfreaks.com/topic/312161-php8-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-int/#findComment-1584542 Share on other sites More sharing options...
ohno Posted January 20, 2023 Author Share Posted January 20, 2023 I forgot to update this (found this thread while searching for something else!). I fixed it by changing line 601 to page = null; Quote Link to comment https://forums.phpfreaks.com/topic/312161-php8-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-int/#findComment-1604853 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.