php mysql_fetch_array problem

Discussion in 'PHP' started by squishi, Apr 1, 2008.

  1. #1
    Hi,

    I programmed a database query some months ago and then had to go offline for a while.
    Now I find that I left the project unfinished with some bug that I can't see right now.

    Here's the code:
    $_query = sprintf("SELECT SQL_CALC_FOUND_ROWS id, title, text
    			FROM articles
    			WHERE status = 'approved'
    			ORDER BY id
    			LIMIT %d,%d",
                SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit()
                );
    
            $_result = mysql_query($_query);
    
            while ($_row = mysql_fetch_array($_result, MYSQL_ASSOC)) {
                // collect each record into $_data
                $_data[] = $_row;
            }
    PHP:
    The error message is:
    It points to the first line of the while loop. I know the data is there, so $_row is not an empty result, I think.

    Any help, please?
     
    squishi, Apr 1, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Typically you get that error when the query did not return a resource, normally because it failed or there were no rows.

    Try: echo mysql_num_rows($_result); and see if the query is returning any rows.
     
    jestep, Apr 1, 2008 IP
  3. singh.ajit05

    singh.ajit05 Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes this problem generraly generated in case of no data.....
    Try with some condition .
    In case of true mysql_fetch_array executed otherwise do nothing
     
    singh.ajit05, Apr 2, 2008 IP
  4. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It does not occur because there is no data. Most probably the query returned an error.

    Change the query line to

    $_result = mysql_query($_query) or die("MySQL error - ".mysql_errno()." - ".mysql_error());
    PHP:
    To see if the query you constructed is valid, just do an echo of $query.
     
    CreativeClans, Apr 2, 2008 IP