middlelady
middlelady

Reputation: 583

Wordpress MySQL - Query doesn't read value = '0'

I'm working a query for notifications about new messages. When a user send a message and the receiver has not still read it, the column read has value 0. Of course when the receiver clicks on the message and reads it the database update the column read with value = 1.

My problem is that using this query:

$check_notification = $wpdb->get_results("
select *
  from {$wpdb->prefix}vp_pms_messages 
  join {$wpdb->prefix}vp_pms_group_users 
    on {$wpdb->prefix}vp_pms_messages.id = {$wpdb->prefix}vp_pms_group_users.message_id 
 where {$wpdb->prefix}vp_pms_group_users.to_username = '{$session_uid}' 
   and {$wpdb->prefix}vp_pms_group_users.read = '0' 
 group 
    by {$wpdb->prefix}vp_pms_group_users.group_id");

{$wpdb->prefix}vp_pms_group_users.read = '0' is not showing any message while if I change the value with '1', I get all the messages read.

It's not a database issue because I see through my phpmyadmin that there are messages with values 0 and messages with value 1.

Is it a formatting problem?

Upvotes: 0

Views: 53

Answers (1)

Raunak Gupta
Raunak Gupta

Reputation: 10809

As you are comparing Zero as a String it might creating a problem, try to type cast vp_pms_group_users.read to integer.

So replace 9 number line with this. and CAST({$wpdb->prefix}vp_pms_group_users.read AS UNSIGNED) = 0

If that field only contains digits then, I'll personality suggest to change the datatype of vp_pms_group_users.read from varcharto tinyint(1) so that you dont have to typecast all the time. (only if it is in development stage, otherwise ignore this suggestion).

Upvotes: 1

Related Questions