Answer the question
In order to leave comments, you need to log in
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 []:
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
$query = $db->prepare("SELECT * FROM `followers` WHERE `author_id`=? AND follower_id=?");
$query->execute([$authorsID,$followersID]);
if ($query->rowCount() > 0) {
echo 'subscribed';
} else {
echo 'unsubscribed';
}
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 questionAsk a Question
731 491 924 answers to any question