Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. PHP 8.1 Count issue

PHP 8.1 Count issue

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
helpphpdatabasemysqltesting
6 Posts 3 Posters 14 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aruna KN
    wrote on last edited by
    #1

    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);

    R L 2 Replies Last reply
    0
    • A Aruna KN

      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);

      R Online
      R Online
      Richard Deeming
      wrote on last edited by
      #2

      You haven't explained what "not working" means. The only notable difference documented in that function between v7 and v8 is:

      PHP: count - Manual[^]:

      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

      A 1 Reply Last reply
      0
      • A Aruna KN

        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);

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        What does "not working" mean, what are you trying to count?

        1 Reply Last reply
        0
        • R Richard Deeming

          You haven't explained what "not working" means. The only notable difference documented in that function between v7 and v8 is:

          PHP: count - Manual[^]:

          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

          A Offline
          A Offline
          Aruna KN
          wrote on last edited by
          #4

          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

          R 1 Reply Last reply
          0
          • A Aruna KN

            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

            R Online
            R Online
            Richard Deeming
            wrote on last edited by
            #5

            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

            A 1 Reply Last reply
            0
            • R Richard Deeming

              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

              A Offline
              A Offline
              Aruna KN
              wrote on last edited by
              #6

              Yes it worked!! Thank you!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups