Help : Warning: mysql_fetch_array()

Discussion in 'MySQL' started by rajivv, Feb 14, 2011.

  1. #1
    Hi,

    My site on one page is giving error like this.

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/......./public_html/library/utility_functions.php on line 2828

    Lines from that file on 2828

    $sql_query="select category_name from ".TABLE_CATEGORY." where id='$catid' " ;
    $sql_ex=mysql_query($sql_query);
    $row =mysql_fetch_array($sql_ex);;
    return $row['category_name'];

    Please help...

    Rajiv
     
    rajivv, Feb 14, 2011 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    1. Can you echo query, and try to run it manually to check what it outputs?
    2. It could be that query has error OR query returns NULL which is why this error comes.

    Try following code.

    
    $sql_query="select category_name from ".TABLE_CATEGORY." where id='$catid' " ;
    $sql_ex=mysql_query($sql_query);
    if($sql_ex && mysql_num_rows($sql_ex) > 0)
    {
        $row =mysql_fetch_array($sql_ex);
        return $row['category_name'];
    }
    
    PHP:
     
    mastermunj, Feb 14, 2011 IP
  3. rajivv

    rajivv Peon

    Messages:
    335
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi got this error in phpmyadmin when I ran ...

    SQL query:

    $sql_query = "select category_name from ".TABLE_CATEGORY. " where id='$catid' ";


    MySQL said:

    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$sql_query="select category_name from ".TABLE_CATEGORY." where id='$catid' "' at line 1
     
    rajivv, Feb 14, 2011 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Nah, that is wrong!

    Anyways, please try the code changes I have suggested first, if it does not work, we will get into query check.
     
    mastermunj, Feb 14, 2011 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    If you look at the error it is outputting the full php code for the query. Not sure why, but it isn't parsing the code before executing it. TABLE_CATEGORY should be replaced with the table name and $catid should be some number and not a variable string.
     
    jestep, Feb 15, 2011 IP
  6. krazzy00

    krazzy00 Guest

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi
    use this instead of ur code.
     
    krazzy00, Feb 16, 2011 IP
  7. rajivv

    rajivv Peon

    Messages:
    335
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $sql_query="select category_name from '".TABLE_CATEGORY."' where id=".$catid." " ;
    $sql_ex=mysql_query($sql_query);
    $row =mysql_fetch_array($sql_ex);;
    return $row['category_name'];

    Error is showin for line $row =mysql_fetch_array($sql_ex);;
    Above is the code used
     
    rajivv, Feb 16, 2011 IP
  8. lapdancer

    lapdancer Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    check you table.... or is the double ;; after the )
     
    lapdancer, Feb 16, 2011 IP
  9. rajivv

    rajivv Peon

    Messages:
    335
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I use this result is empty nothing in front end no error but no result

    $sql_query="select category_name from '".TABLE_CATEGORY."' where id=".$catid." " ;

    $sql_ex=mysql_query($sql_query);
    if($sql_ex && mysql_num_rows($sql_ex) > 0)
    {
    $row =mysql_fetch_array($sql_ex);;
    return $row['subcat_name'];
    }
    return '';
     
    rajivv, Feb 16, 2011 IP
  10. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #10
    You need:

    return $row['category_name'];

    and not

    return $row['subcat_name'];
     
    jestep, Feb 17, 2011 IP