0

I created a function to recall the amount of posts in a certain category, in a forum. Here is the code for the function:

function num_posts_evedisc() {
    $sql3 = "SELECT COUNT(category) FROM forum_question WHERE category=3";
    $query3 = mysql_query($sql3);
    return($query3);
}

And this is the response I got, echo-ing it:

Resource id #14

(In my database, I have table forum_question and the column category. I tried replacing the (category) with (id) and that didn't work either.)

Thanks!

asbxzeeko
  • 71
  • 7
  • 1
    It's a result resource - you need to get the data with `mysql_fetch_array` or similar. – Jon Apr 15 '13 at 23:57
  • The mysql_* extensions have been deprecated: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – doublesharp Apr 15 '13 at 23:59

2 Answers2

3

mysql_query returns a resource, which in turn gives you access to the result. To access the result, you have to use something to get information from the resource, such as:

if ($row = mysql_fetch_row($query3)) {
    return $row[0];
}

(You can also use other functions, like mysql_fetch_array or mysql_result.)

Note, though, that the mysql_* functions are deprecated and will be removed in a future version of PHP. Look at Mysql Improved Extension or PDO_MYSQL.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks that worked. Also, I know they are deprecated, I just will update everything once I've finished most of the site. – asbxzeeko Apr 16 '13 at 00:02
1
return mysql_result($query3, 0) 

is what you need to return

dave
  • 62,300
  • 5
  • 72
  • 93