0

I need to count the number of instances of a word in a column from my database and then get a true or false result depending on if it exceeds 36 instances. I use wordpress and I know that the connecction to the database is correct. And I am using wordpress.

This is what I got this far but its not working:

$selected = mysql_select_db("bringes_phpbb3", $dbConn) or die("Could not select database. The error was ".mysql_error());
    mysql_query("SET NAMES utf8");


    $SQL_COUNT ="SELECT COUNT(field_name) AS total_number FROM cyklister WHERE grupp LIKE CONCAT ('%','word','%')";
            $result = mysql_query($SQL_COUNT);

         if ($result >= 36){
         $awnser = true;


    mysql_free_result($result);

    mysql_close($dbConn);

The SQL query is not complete or may be far from what I am looking for. Can someone help me?

Aitazaz Khan
  • 1,609
  • 1
  • 13
  • 35
JK Web
  • 3
  • 2
  • 2
    Have you checked the documentation for `mysql_query` function? Why haven't you? – zerkms Oct 23 '14 at 07:26
  • What zerkms means is that the result from `mysql_query` is not what you expect. It's a handle to the result, not the result itself. – KIKO Software Oct 23 '14 at 07:31
  • Not a duplicate, but this question will probably help you out: http://stackoverflow.com/questions/6907751/select-count-from-table-of-mysql-in-php – Supericy Oct 23 '14 at 07:31

1 Answers1

0

After executing mysql_query, you need to call mysql_fetch_array to retrieve the result:

$result = mysql_query($SQL_COUNT);
$row = mysql_fetch_array($result);
$count = $row[0];

if ($count >= 36) {
    $awnser = true;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Okey. But i still need to get the query right. i got it from another code but do not know what to put where it says "field_name". And do you think that it otherwise will give me only the number of instances? – JK Web Oct 23 '14 at 07:59
  • @Mureinik have clear to you it will work fine you just have to know which field you wanna count down and the answer is correct – Aitazaz Khan Oct 23 '14 at 08:13
  • So field is the column? i have already mentioned the column in thequery, should it be in two places? – JK Web Oct 23 '14 at 08:15
  • i have tried using this query in phpmyadmin with the result i wanted: SELECT COUNT(grupp) AS total_number FROM cyklister WHERE grupp LIKE CONCAT ('%','1','%') but when i run the complete code in a wordpress templete file with the last finish from Mureinik it always gives me a "0" in result when i echo $count. I do only preview it in wordpress. Does it have to be published to work? – JK Web Oct 23 '14 at 09:10
  • Now I am getting close. The code is working almost right. When i echo the code it gives me the right value. But when i put it together with the rest of my code it shold turn a flag from false to true instead. But as soon as i run the code it gives me a blank page. This is how it looks now after the query: $result = mysql_query($SQL_COUNT); $row = mysql_fetch_array($result); $count = $row[0]; elseif ($count == 36){ $full=true; } – JK Web Oct 23 '14 at 10:15