PDA

View Full Version : mysql_fetch_array() error? please help me. :)


DefineItFast
06-15-2008, 05:11 PM
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>";
}

I thought my code was correct now but I had this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

The line with error is this line:
$totalnoun = mysql_fetch_array($countnoun);

What's wrong with code? The echo for the total cound value works but the count for the nound doesn't.:confused: Thank you in advance for trying to help me. :)

PS
By the way, I am new here. Glad to be here.:thumbsup:

bdl
06-15-2008, 05:30 PM
Use mysql_error() to trap the error. You'll find that the second query (the one you're referring to) is an invalid statement.


You've got a WHERE clause inside your COUNT, which is invalid. What you're probably looking to do is more like this:

SELECT count(*)
FROM wn_synset
WHERE word="{input}"
AND ss_type="n"


You could technically use a subquery there, but I doubt it's necessary.

I also strongly suggest you take the calls to str_replace() out of your SQL statement, assign those values above the queries and use the variable in the statement. It's also a Good Idea to assign the SQL statement to a separate variable and pass that to the mysql_query() call, so you can view the exact statement sent to MySQL and troubleshoot any problems.

DefineItFast
06-15-2008, 05:43 PM
Thank you for the tips. I moved the str_replace() out of the SQL statement and I also edit my mysql_query into this:
$cnoun = mysql_query('SELECT count(ss_type) FROM wn_synset WHERE word="'.$word.'" AND AND ss_type="n"');

but I still got the same error on the same line. What do you mean on
It's also a Good Idea to assign the SQL statement to a separate variable and pass that to the mysql_query() call, so you can view the exact statement sent to MySQL and troubleshoot any problems.

I am sorry, I am just learning PHP. :) Thanks for teaching me. :D

PS
It worked now, I entered 2 "AND" in mysql_query. :) LOL my fault.