I have a code that needs to count for the total value and each value that has matched in the column. Please the code: //Parts of Speech, contains in ss_type for echo $part_speech = array("n"=>"n.","v"=>"verb", "r"=>"adv./adj.", "a"=>"adj.", "s"=>"adj."); $counting = mysql_query('SELECT count(ss_type) FROM wn_synset WHERE word="'.str_replace(' ', '_', $word).'"'); $countnoun = mysql_query('SELECT count(WHERE ss_type="n") FROM wn_synset WHERE word="'.str_replace(' ', '_', $word).'"'); $total = mysql_fetch_array($counting); $totalnoun = mysql_fetch_array($countnoun); $count = 0; $countnoun = 0; //this is in a loop $count = $count + 1; $countnoun = $countnoun + 1; echo "Total: $total[0]<br />"; if ($totalnoun[0] > 0) { echo "-$totalnoun[0] nouns</h2>"; } PHP: I thought my code was correct now but I had this error: The line with error is this line: $totalnoun = mysql_fetch_array($countnoun); PHP: What's wrong with code? The echo for the total cound value works but the count for the nound doesn't.Thank you in advance for trying to help me.
try this $totalnoun = @mysql_fetch_array($countnoun); If the code is ok it will remove warning but be sure that code is ok Regards Alex
The @ is simply error suppression, so even though it wants to tell you that it doesn't have a valid result resource, it won't show up at all. Usually I see this error being caused because the query has an error in it. Add an or die('Error: '.mysql_error()) to the end of the query with the problem and see if it's actually valid or if there is a problem with your query. I've honestly never seen "count(WHERE ss_type="n")" before, generally you do count(*) or count(field) (if you have NULL values count(field) will ignore them so it's faster than * in that scenario). I know there are generally 500 ways to do the same thing in SQL, I'm not sure if what you wrote is proper but I just don't see it like that. That's where i'd start checking =)
Supressing errors wont help solve the problem only hide them. you should change $countnoun = mysql_query('SELECT count(WHERE ss_type="n") FROM wn_synset WHERE word="'.str_replace(' ', '_', $word).'"'); Code (markup): to $countnoun = mysql_query('SELECT count(ss_type) FROM wn_synset WHERE ss_type="n" AND word="'.str_replace(' ', '_', $word).'"'); Code (markup): you can also use mysql_num_rows() to see if there are valid records pulled back like if(mysql_num_rows($countnoun) > 0) { //proceed } else { echo "No results found"; } Code (markup):
supplied argument is not a valid MySQL result resource normally means it is trying to break results up into an array, when there are no results
Here's a little code modification //Parts of Speech, contains in ss_type for echo $part_speech = array("n"=>"n.","v"=>"verb", "r"=>"adv./adj.", "a"=>"adj.", "s"=>"adj."); $counting = mysql_query('SELECT count(ss_type) FROM wn_synset WHERE word="'.str_replace(' ', '_', $word).'"'); $countnoun = mysql_query('SELECT count(WHERE ss_type="n") FROM wn_synset WHERE word="'.str_replace(' ', '_', $word).'"'); // mod here $total = ($counting) ? mysql_fetch_array($counting) : 0; $totalnoun = ($countnoun) ? mysql_fetch_array($countnoun) : 0; $count = 0; $countnoun = 0; //this is in a loop $count = $count + 1; $countnoun = $countnoun + 1; echo "Total: $total[0]<br />"; if ($totalnoun[0] > 0) { echo "-$totalnoun[0] nouns</h2>"; }[ PHP:
Thanks for the help guys! Thanks for the modification of the script xrvel! lfhost is correct, the cause of the error is in the mysql_query in the variable $countnoun. the count() function was incorrect. It should be like this: $countnoun = mysql_query('SELECT [B]count(ss_type)[/B] FROM wn_synset WHERE word="'.str_replace(' ', '_', $word).'" [B]AND ss_type="n"[/B]'); PHP: By the way, I will try the code you modified also xrvel. Thank you.