S
S
Shamil Khairulaev2021-04-17 23:18:16
PHP
Shamil Khairulaev, 2021-04-17 23:18:16

How to make json in mysql empty so that rowCount returns 0?

I need to know when there are no values ​​in mysql table json, so I check it with rowCount. But after some value has been in it and I delete it from there, [] remains there and now rowCount is never equal to zero. What needs to be done for this? Or maybe I'm completely wrong with json in mysql?

Screenshot of table in phpMyAdmin with value []:
607b4141ab8f4257507843.png

My code I use to handle json in mysql

//Извлекаем json со списком подписчиков
      $query = $db->prepare(
         "SELECT `followers` FROM `users` WHERE `id`='$authorsID'"
      );
      $query->execute();
      $followers = null;

      //Если в строка пустая
      if ($query->rowCount() > 0) {
         $followers = (array) json_decode($query->fetch()[0]);

         //Если нажимающий кнопку не подписан на автора
         if (!in_array($followersID, $followers)) {
            $followers[$followersID] = $followersID;
            echo 'subscribed';

            //Если подписан
         } else {
            unset($followers[$followersID]);
            echo 'unsubscribed';
         }
         //Если json пустой(нет подписчиков)
      } else {
         $followers = [$followersID => $followersID];
         echo 'subscribed';
      }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FanatPHP, 2021-04-18
@ComPUCKter

$query = $db->prepare("SELECT * FROM `followers` WHERE `author_id`=? AND follower_id=?");
$query->execute([$authorsID,$followersID]);

and lo and behold! rowCount() suddenly works!!!!
if ($query->rowCount() > 0) {
      echo 'subscribed';
} else {
      echo 'unsubscribed';
}

Y
Yupiter7575, 2021-04-17
@yupiter7575

As I understand it, you need to check the array for emptiness? A good method is isEmpty. There are other methods

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question