1

I am trying to retrieve from db the total number of pages written by a certain user using the below code:

function getPagesNoById($id) {
    if ($id) {
        $sql_query = "SELECT COUNT(page_id) FROM `texts` WHERE `user_id` = '" . $id . "'";
        $query = mysql_query($sql_query)or die(mysql_error());
        $cnt = mysql_num_rows($query);
        mysql_close($conn);
        $result = mysql_query($query);
        return $result;
    }
    return false;
}

print getPagesNoById(7);

But the result is empty. Is there something wrong with it ?

Thanks in advance

em0tic0n
  • 292
  • 2
  • 16
  • 1
    @marc-b as i said the code isnt running with or without the quotes so i dont see any relevance of why you have marked as duplicate as the main question is other!! – em0tic0n Aug 11 '15 at 16:52

3 Answers3

2

Try this :

function getPagesNoById($id) {
    if ($id) {
        $sql_query = "SELECT COUNT(page_id) FROM `texts` WHERE `user_id` = '" . $id . "'";
        $result = mysql_query($sql_query) or die(mysql_error());
        $row = mysql_fetch_array($result);
        $total = $row[0];
        return $total;
    }
    return false;
}
Jean Bob
  • 565
  • 10
  • 24
0

Try this:

SELECT COUNT(page_id) FROM ...

Don't use the '-char in MySQL for column selection - 'is constant'. For this use the `-char or nothing as long there is no whitespace or any other special char in your colname...

Benvorth
  • 7,416
  • 8
  • 49
  • 70
  • Tried also $sql_query = "SELECT COUNT(page_id) FROM `texts` WHERE `user_id` = '" . $id . "'"; . The same empty. – em0tic0n Aug 11 '15 at 16:33
0

try to change,

 $sql_query = "SELECT COUNT('page_id') FROM `texts` WHERE `user_id` = '" . $id . "'";

to

 $sql_query = "SELECT COUNT(page_id) FROM `texts` WHERE `user_id` = '" . $id . "'";

and also remove

  mysql_close($conn);

in the middle

  • Tried the same empty response. In php my admin : SELECT COUNT(page_id) FROM `texts` WHERE `user_id` = '7'; returns 93 – em0tic0n Aug 11 '15 at 16:34