mysql_fetch_array() error? please help me. :)

Discussion in 'PHP' started by d1srupt3r, Jun 15, 2008.

  1. #1
    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.:confused:Thank you in advance for trying to help me. :)
     
    d1srupt3r, Jun 15, 2008 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    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
     
    kmap, Jun 15, 2008 IP
  3. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The error was gone but it doesn't echo the $totalnoun[0].. What is "@" for? Thanks for the help. :)
     
    d1srupt3r, Jun 15, 2008 IP
  4. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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 =)
     
    projectshifter, Jun 15, 2008 IP
  5. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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):
     
    lfhost, Jun 15, 2008 IP
  6. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #6
    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
     
    crath, Jun 15, 2008 IP
  7. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #7
    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:
     
    xrvel, Jun 15, 2008 IP
  8. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    d1srupt3r, Jun 16, 2008 IP