SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2003
    Location
    London
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql function error when called from an include file

    HELP!

    Can anyone shed any light on the reason why the function below works perfectly fine when called from the page it resides in but when placed in an include file using require_once throws back the following error:

    mysql_fetch_array(): 8 is not a valid MySQL result resource


    I am using a very basic dataLayer extraction class that wraps up the mySQL functions into generic named functions.

    Any help/pointers would be great.

    Thanks

    Keir

    /* --- Function to Create Gallery Map outline --- */
    function funcCreateGalleryMap( $parent_ID, $level )
    {

    $dataLayer = new dataLayer();

    $gallerymap_sql = "SELECT * FROM gallery_categories WHERE parent_ID = ".$parent_ID." ORDER BY cat_title ASC";


    $dataLayer->connect(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);
    $dataLayer->array_type = MYSQL_ASSOC;
    $gallerymap_query = $dataLayer->query( $gallerymap_sql );

    while( $rs_gallerymap = $dataLayer->fetch_array( $gallerymap_query ) )
    {
    echo ( $level." - ".$rs_gallerymap["cat_title"]."<br />" );

    /* Recursion */
    funcCreateGalleryMap( $rs_gallerymap["ID"], $level+1 );

    };

    $dataLayer->close();

    }

  2. #2
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Where does dataLayer reside?
    What errors do you get when you run it with error reporting on high ( error_reporting(E_ALL) ) ?

    I assume the call to mysql_fetch_array is in dataLayer?

    I do a var_dump on $dataLayer and $gallerymap_query to see what's in them.

    eg: var_dump($gallerymap_query);

    --ed

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2003
    Location
    London
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    coo_t2

    Thanks for the tips. You are spot on about the mysql_fetch_array being included as part of the dataLayer class. This is included into the file as well.

    I have done a var_dump as you suggest and get the following back:

    SELECT * FROM gallery_categories WHERE parent_ID = 0 ORDER BY cat_title ASC
    resource(6) of type (mysql result) 0 - 22
    SELECT * FROM gallery_categories WHERE parent_ID = 10 ORDER BY cat_title ASC
    resource(8) of type (mysql result) 1 - 33
    SELECT * FROM gallery_categories WHERE parent_ID = 11 ORDER BY cat_title ASC
    resource(10) of type (mysql result)
    Warning: mysql_fetch_array(): 8 is not a valid MySQL result resource in /home/******/public_html/admin/classes/datalayer.class.php on line 51
    0 - test
    SELECT * FROM gallery_categories WHERE parent_ID = 9 ORDER BY cat_title ASC
    resource(12) of type (mysql result)

    They all seem to be of type mysql result. THe strange thing is this proves the query actually runs as the error appears half way through, just before it goes back to the beginning of the tree. Do you think it is something to do with the recursion?

  4. #4
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by keir
    Do you think it is something to do with the recursion?
    I forgot about the recursion. Yes I'm sure this could screw things up because you're opening and closing DB connections in the recursive function. Try opening and closing the DB connection only once outside of the recursive function. I guess this mean you have to use only one dataLayer object repeatedly, hopefully the object is set up that way.

    --ed

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    while( $rs_gallerymap $dataLayer->num_rows$gallerymap_query ) ) 

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2003
    Location
    London
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    reminder

    Thanks again, same problem different function. For some reason this time your suggestion doesn't work. I appear to get stuck in an infinite loop.

    The problem only occurs when the function is called from an included file. It also only seems to kick in when the recursion takes place. Strangely it still outputs the correct value but leaves an error there as well. I am wondering if this is a bug in PHP as it works fine on my development server and when called from within the same page. Can't understand why it works as intended in one place but not another on the same server?

    Thanks for all the tips.

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wrong code
    PHP Code:
    function funcCreateGalleryMap$parent_ID$level )
    {
    $dataLayer = new dataLayer();
    $gallerymap_sql "SELECT * FROM gallery_categories WHERE parent_ID = ".$parent_ID." ORDER BY cat_title ASC";
    $dataLayer->connect(MYSQL_SERVERMYSQL_USERNAMEMYSQL_PASSWORDMYSQL_DATABASE);
    $dataLayer->array_type MYSQL_ASSOC;
    $gallerymap_query $dataLayer->query$gallerymap_sql );
    while( 
    $rs_gallerymap $dataLayer->fetch_array$gallerymap_query ) ) 
    {
    echo ( 
    $level." - ".$rs_gallerymap["cat_title"]."<br />" );
    /* Recursion */
    funcCreateGalleryMap$rs_gallerymap["ID"], $level+);
    };
    $dataLayer->close();

    right code
    PHP Code:
    function funcCreateGalleryMap$parent_ID$level )
    {
    $dataLayer = new dataLayer();  //better if u place
    $dataLayer->connect(MYSQL_SERVERMYSQL_USERNAMEMYSQL_PASSWORDMYSQL_DATABASE);  // those 3
    $dataLayer->array_type MYSQL_ASSOC//lines into config.php or core.php
    $gallerymap_sql "SELECT * FROM gallery_categories WHERE parent_ID = ".$parent_ID." ORDER BY cat_title ASC";
    $gallerymap_query $dataLayer->query$gallerymap_sql );
    while( 
    $rs_gallerymap $dataLayer->fetch_array$gallerymap_query ) ) 
    {
    echo ( 
    $level." - ".$rs_gallerymap["cat_title"]."<br />" );
    /* Recursion */
    funcCreateGalleryMap$rs_gallerymap["ID"], $level+);
    };
    $dataLayer->close();

    better way code
    PHP Code:
    function funcCreateGalleryMap$datalayer$parent_ID$level )
    {
    $gallerymap_sql "SELECT * FROM gallery_categories WHERE parent_ID = ".$parent_ID." ORDER BY cat_title ASC";
    $gallerymap_query $dataLayer->query$gallerymap_sql );
    $rs_gallerymap $dataLayer->num_rows$gallerymap_query );
    echo 
    $level." - ".$rs_gallerymap["cat_title"]."<br />";
    /* Recursion */
    funcCreateGalleryMap$rs_gallerymap["ID"], $level+);
    $dataLayer->free_result();

    cheers

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2003
    Location
    London
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    reminder

    Many thanks for taking the time to look at this. I look forward to trying these out later tonight.

    Keir


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •