PHP 8.1 Count issue
-
I'm testing my old PHP code with PHP 8.1 (code is working fine with PHP 7.4). Following line is not working on PHP 8.1
$total += count($row[0]);
I tried with strlen but it gives totally wrong result because this count is counting messages, size of inbox.
if (!defined("_COMMON_")) {echo "stop";exit;}
include_once($ld_engine_path."inc_connect.php");
$m_result = mysqli_query($conn,"select new_mails from ".$mysql_table_prefix."users where id=$is_regist") or die("database error
".mysqli_error($conn));
if (mysqli_num_rows($m_result))
list($new_board_messages) = mysqli_fetch_array($m_result, MYSQLI_NUM);
if (!isset($new_board_messages)) $new_board_messages = "0";
mysqli_free_result($m_result);$m_result = mysqli_query($conn,"select concat(subject,body) from ".$mysql_table_prefix."board where user_id=$is_regist") or die("database error: cannot retrieve mail-messages
".mysqli_error($conn));
$total = 0;
while ($row = mysqli_fetch_array($m_result, MYSQLI_NUM))
$total += count($row[0]); // This line is not working on PHP 8.1$percentage = round($total / $max_mailbox_size *100);
mysqli_free_result($m_result); -
I'm testing my old PHP code with PHP 8.1 (code is working fine with PHP 7.4). Following line is not working on PHP 8.1
$total += count($row[0]);
I tried with strlen but it gives totally wrong result because this count is counting messages, size of inbox.
if (!defined("_COMMON_")) {echo "stop";exit;}
include_once($ld_engine_path."inc_connect.php");
$m_result = mysqli_query($conn,"select new_mails from ".$mysql_table_prefix."users where id=$is_regist") or die("database error
".mysqli_error($conn));
if (mysqli_num_rows($m_result))
list($new_board_messages) = mysqli_fetch_array($m_result, MYSQLI_NUM);
if (!isset($new_board_messages)) $new_board_messages = "0";
mysqli_free_result($m_result);$m_result = mysqli_query($conn,"select concat(subject,body) from ".$mysql_table_prefix."board where user_id=$is_regist") or die("database error: cannot retrieve mail-messages
".mysqli_error($conn));
$total = 0;
while ($row = mysqli_fetch_array($m_result, MYSQLI_NUM))
$total += count($row[0]); // This line is not working on PHP 8.1$percentage = round($total / $max_mailbox_size *100);
mysqli_free_result($m_result);You haven't explained what "not working" means. The only notable difference documented in that function between v7 and v8 is:
Prior to PHP 8.0.0, if the parameter was neither an array nor an object that implements the Countable interface, 1 would be returned, unless value was null, in which case 0 would be returned.
So either you're now getting a
TypeError
where previously you were just counting the number of rows; you're now getting the sum of the string lengths, whereas previously you were counting the number of rows; or something else is happening, which you haven't explained.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I'm testing my old PHP code with PHP 8.1 (code is working fine with PHP 7.4). Following line is not working on PHP 8.1
$total += count($row[0]);
I tried with strlen but it gives totally wrong result because this count is counting messages, size of inbox.
if (!defined("_COMMON_")) {echo "stop";exit;}
include_once($ld_engine_path."inc_connect.php");
$m_result = mysqli_query($conn,"select new_mails from ".$mysql_table_prefix."users where id=$is_regist") or die("database error
".mysqli_error($conn));
if (mysqli_num_rows($m_result))
list($new_board_messages) = mysqli_fetch_array($m_result, MYSQLI_NUM);
if (!isset($new_board_messages)) $new_board_messages = "0";
mysqli_free_result($m_result);$m_result = mysqli_query($conn,"select concat(subject,body) from ".$mysql_table_prefix."board where user_id=$is_regist") or die("database error: cannot retrieve mail-messages
".mysqli_error($conn));
$total = 0;
while ($row = mysqli_fetch_array($m_result, MYSQLI_NUM))
$total += count($row[0]); // This line is not working on PHP 8.1$percentage = round($total / $max_mailbox_size *100);
mysqli_free_result($m_result); -
You haven't explained what "not working" means. The only notable difference documented in that function between v7 and v8 is:
Prior to PHP 8.0.0, if the parameter was neither an array nor an object that implements the Countable interface, 1 would be returned, unless value was null, in which case 0 would be returned.
So either you're now getting a
TypeError
where previously you were just counting the number of rows; you're now getting the sum of the string lengths, whereas previously you were counting the number of rows; or something else is happening, which you haven't explained.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You are correct, previously it was counting number of rows, now it's counting string lenghths which is not the requirement. Please suggest me a solution
Replace:
$total += count($row[0]);
with:
$total += 1;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Replace:
$total += count($row[0]);
with:
$total += 1;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer