For example I have table "items" with row "goods" where I have some number of word "book" ... so how can I count it using php?
Have you ever used MySQL with PHP before? I just want to make sure you know that you need to use that function jazzcho provided along with a queryID. Something along the lines of this: $result = mysql_query("SELECT * FROM items"); $number_of_rows = mysql_num_rows($result); PHP: There is another way of doing it by selecting COUNT(*) from your mysql table instead of getting all the records, but I feel this is easier to wrap your head around. I am a little confused about your table though. Are you simply trying to figure out how many entries there are in the table "items"?
<? $cnx = mysql_connect('localhost', 'root', 'mypass'); mysql_select_db('mydb'); $word = 'book'; $query = mysql_query("SELECT goods FROM items"); while( $fetch = mysql_fetch_array( $query ) ): $matrix = explode(' ', $fetch['goods']); $count = 0; foreach( $matrix as $w ): if( $w==$word ) $count++; endforeach; print $count.'<br />'; endwhile; ?>
You should split the row by " " (space) and compare all words by the word that you want to count... if the word == yourword so count ++... Excuse my english... buena suerte!
this will find the exact word book in each row.. it will disregard the words booking,booker,etc.. // ... the query statement here... $results = array(); while($row = mysql_fetch_assoc($result)) { $word_count = preg_match_all("/\bbook\b/",$row['goods']); $row['word_count'] = $word_count?$word_count:0; $results[] = $row; } PHP: if you want to find every instance of the work "book", just remove the \b at the start and end of it..
It gives me the error Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/public_html/count2.php on line 10
<? $cnx = mysql_connect('localhost', 'root', 'mypass'); mysql_select_db('mydb'); $words = array('book', 'dog', 'cat', 'computer', 'metal'); $matrix_word = array(); $query = mysql_query("SELECT goods FROM items"); while( $fetch = mysql_fetch_array( $query ) ): $matrix = explode(' ', $fetch['goods']); foreach( $matrix as $w) if( in_array($w, $words) ) $matrix_word[$w]++; endwhile; foreach( $words as $word ) print $word.' - '.(int)$matrix_word[$word].' <br />'; ?> PHP: This return something like: book - 8 dog - 2 cat - 0 computer - 0 metal - 1 What do you think about it?
There is another way to count same words in a row, but I don't know how to sort results by quantity, would you help me? $sql = "SELECT items, count(*) cs FROM users GROUP BY items LIMIT 10"; $result = mysql_query($sql) or die(mysql_error()); while ($a = mysql_fetch_assoc($result)) { echo $a["items"]." - ". $a["cs"]." - "; } PHP:
you should replace $results with the variable of your mysql_query return.. $result = mysql_query("SELECT * FROM `items`"); $results = array(); while($row = mysql_fetch_assoc($result)) { $word_count = preg_match_all("/\bbook\b/",$row['goods']); $row['word_count'] = $word_count?$word_count:0; $results[] = $row; } PHP:
Thanks! But solution has been found already ... your example I will have just in case ))) ... Would you help me with my previous question?
solution found ))) $sql = "SELECT country, count(*) AS cs FROM users GROUP BY country ORDER BY cs DESC"; PHP: