wp custom table – count wp rows returns 0

I made a custom wp table,
but there is a problem. Count rows doesn’t work carefully.

All of this variable return 0,
but you can see in my table we have 3 record with read = 1.

Read More

(read=1 : read ,read = 2 : unread)

$table_name    = $wpdb->prefix . 'inbox';

$inbox_rows = $wpdb->get_var("SELECT COUNT(*) FROM $table_name where type= 1 ");
$unread1 = $wpdb->get_var("SELECT COUNT(*) FROM $table_name WHERE 'read'= 1 and type = 1 ");
$unread2 = $wpdb->get_var("SELECT COUNT(*) FROM $table_name WHERE 'read'= 1 and type = 2 ");
$result = $wpdb->get_var("SELECT COUNT(*) FROM $table_name where  'read'= 1 ");

/*
|------|------|
| read | type | both of them are int (in phpmyadmin)
|------|------|
|   2  |  1   |
|------|------|
|   1  |  1   |
|------|------|
|   1  |  2   |
|------|------|
|   1  |  1   |
|------|------|

*/

Please help me.

Related posts

1 comment

  1. I think u missing the $wpdb global declare like below code:

     global $wpdb;
        $table_name    = $wpdb->prefix . 'index';
        $inbox_rows = $wpdb->get_var("SELECT COUNT(*) FROM $table_name where type= 1 ");
    

Comments are closed.