Results 1 to 4 of 4

Thread: Lost connection to MySQL server during query [2013]

  1. #1

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Lost connection to MySQL server during query [2013]

    this is driving me nuts..
    I had this working fine with phpbb3 and a mod called portal...
    now, it wont work.. it seems like its timing out or something.

    It an rss feed parser.. pulls them in and dumps them to the page
    no matter where I call the class and pass in the feeds it fails.

    Strange this is.. wherever i call it.. it will fail on the next sql query

    here is the bb3portal which calls all the files to build the page

    PHP Code:
    <?php

    $phpbb_root_path 
    './';

    define('IN_PHPBB'true);
    $phpEx substr(strrchr(__FILE__'.'), 1);
    include(
    $phpbb_root_path 'common.' $phpEx);
    include(
    $phpbb_root_path 'bb3portal/config.'.$phpEx);
    include(
    $phpbb_root_path 'bb3portal/includes/functions.'.$phpEx);

    $user->session_begin();
    $auth->acl($user->data);
    $user->setup('bb3portal');

    include (
    $phpbb_root_path 'bb3portal/block/rss_feeds.' $phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/rss_feedlist.'.$phpEx);

    // only registered user see blocks
    if ($user->data['is_registered'])
    {
        
    //include($phpbb_root_path . 'bb3portal/block/online_friends.'.$phpEx);
        
    include($phpbb_root_path 'bb3portal/block/user_menu.'.$phpEx);
    }
    else
    {
        include(
    $phpbb_root_path 'bb3portal/block/login_box.'.$phpEx);
    }
    // other blocks
    include($phpbb_root_path 'shout.' $phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/announcements.'.$phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/news.'.$phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/recent.'.$phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/stat_adv.'.$phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/whos_online.'.$phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/latest_members.'.$phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/random_member.'.$phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/most_poster.'.$phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/donate.'.$phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/search.'.$phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/link_us.'.$phpEx);
    include(
    $phpbb_root_path 'bb3portal/block/leaders.'.$phpEx);

    // output page
    page_header($user->lang['PORTAL']);

    $template->set_filenames(array(
        
    'body' => 'bb3portal/portal_main.html')
    );

    make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));

    page_footer();

    ?>
    here is the rss_feeds.php file (the class.. which i know works fine.. i can include this with no issues...
    PHP Code:
    <?php

    class StaticFxRssFeed {
        
        private 
    $nr_news=5;
        private 
    $rss_channel = array();
        private 
    $currently_writing "";
        private 
    $main "";
        private 
    $item_counter 0;
        private 
    $template;
        private 
    $feedname;
        private 
    $url;

        function 
    __construct($feedname$url) {
          
    $this->feedname $feedname;
          
    $this->url $url;
        }
        
        function 
    startElement($parser$name$attrs) {
               switch(
    $name) {
                   case 
    "RSS":
                   case 
    "RDF:RDF":
                   case 
    "ITEMS":
                       
    $this->currently_writing "";
                       break;
                   case 
    "CHANNEL":
                       
    $this->main "CHANNEL";
                       break;
                   case 
    "IMAGE":
                       
    $this->main "IMAGE";
                       
    $this->rss_channel["IMAGE"] = array();
                       break;
                   case 
    "ITEM":
                       
    $this->main "ITEMS";
                       break;
                   default:
                       
    $this->currently_writing $name;
                       break;
               }
        }
        
        function 
    endElement($parser$name) {
               
    $this->currently_writing "";
               if (
    $name == "ITEM") {
                   
    $this->item_counter++;
               }
        }
        
        function 
    characterData($parser$data) {
            if (
    $this->currently_writing != "") {
                switch(
    $this->main) {
                    case 
    "ITEMS":
                        if (isset(
    $this->rss_channel[$this->main][$this->item_counter][$this->currently_writing])) {
                            
    $this->rss_channel[$this->main][$this->item_counter][$this->currently_writing] .= $data;
                        } else {
                            
    //print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
                            
    $this->rss_channel[$this->main][$this->item_counter][$this->currently_writing] = $data;
                        }
                        break;
                }
            }
        }
        
        function 
    get_data(&$template) {
                    
    $xml_parser xml_parser_create();
                    
    xml_set_element_handler
                      
    $xml_parser
                      array(
    $this'startElement'), 
                      array(
    $this'endElement'
                    ); 
                    
    xml_set_character_data_handler
                      
    $xml_parser
                      array(
    $this'characterData'
                    ); 
                    
                    
    $data self::curl_string($this->url);
                    
    xml_parse($xml_parser,$data);
                    
    xml_parser_free($xml_parser);
                    
                    
    // putting in array
                    
    $news=array();
                    if (isset(
    $this->rss_channel["ITEMS"])) 
                    {
                        if (
    count($this->rss_channel["ITEMS"]) > 0
                            for(
    $i 0;$i count($this->rss_channel["ITEMS"]);$i++) $news[]=$this->rss_channel["ITEMS"][$i];
                    }
                    
    $c=0;
                    
                    foreach(
    $news as $key=>$val)
                    {
                        if(
    $c<$this->nr_news)
                        {
                            
    $template->assign_block_vars($this->feedname, array(
                            
    'LINK' => isset($val['LINK']) ? $val['LINK'] : '',
                            
    'TITLE' => isset($val['TITLE']) ? $val['TITLE'] : '',
                            
    'DESC' => isset($val['DESCRIPTION']) ? $val['DESCRIPTION'] : '')
                            );
                        }
                        
    $c++;
                    }
                }
        private static function 
    curl_string ($url,$user_agent='Mozilla 4.0'){

           
    $ch curl_init();

           
    curl_setopt ($chCURLOPT_URL$url);
           
    curl_setopt ($chCURLOPT_USERAGENT$user_agent);
           
    curl_setopt ($chCURLOPT_HEADER0);
           
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);
           
    curl_setopt ($chCURLOPT_FOLLOWLOCATION1);
           
    curl_setopt ($chCURLOPT_TIMEOUT2);
           
    $result curl_exec ($ch);
           
    curl_close($ch);
           return 
    $result;
      
            }
    }


    ?>
    and finally here is the rss_feedlist.php which passes in the urls for the feeds.
    PHP Code:
    <?php
    class StaticFxRssFeed {
        
        private 
    $nr_news=5;
        private 
    $rss_channel = array();
        private 
    $currently_writing "";
        private 
    $main "";
        private 
    $item_counter 0;
        private 
    $template;
        private 
    $feedname;
        private 
    $url;

        function 
    __construct($feedname$url) {
          
    $this->feedname $feedname;
          
    $this->url $url;
        }
        
        function 
    startElement($parser$name$attrs) {
               switch(
    $name) {
                   case 
    "RSS":
                   case 
    "RDF:RDF":
                   case 
    "ITEMS":
                       
    $this->currently_writing "";
                       break;
                   case 
    "CHANNEL":
                       
    $this->main "CHANNEL";
                       break;
                   case 
    "IMAGE":
                       
    $this->main "IMAGE";
                       
    $this->rss_channel["IMAGE"] = array();
                       break;
                   case 
    "ITEM":
                       
    $this->main "ITEMS";
                       break;
                   default:
                       
    $this->currently_writing $name;
                       break;
               }
        }
        
        function 
    endElement($parser$name) {
               
    $this->currently_writing "";
               if (
    $name == "ITEM") {
                   
    $this->item_counter++;
               }
        }
        
        function 
    characterData($parser$data) {
            if (
    $this->currently_writing != "") {
                switch(
    $this->main) {
                    case 
    "ITEMS":
                        if (isset(
    $this->rss_channel[$this->main][$this->item_counter][$this->currently_writing])) {
                            
    $this->rss_channel[$this->main][$this->item_counter][$this->currently_writing] .= $data;
                        } else {
                            
    //print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
                            
    $this->rss_channel[$this->main][$this->item_counter][$this->currently_writing] = $data;
                        }
                        break;
                }
            }
        }
        
        function 
    get_data(&$template) {
                    
    $xml_parser xml_parser_create();
                    
    xml_set_element_handler
                      
    $xml_parser
                      array(
    $this'startElement'), 
                      array(
    $this'endElement'
                    ); 
                    
    xml_set_character_data_handler
                      
    $xml_parser
                      array(
    $this'characterData'
                    ); 
                    
                    
    $data self::curl_string($this->url);
                    
    xml_parse($xml_parser,$data);
                    
    xml_parser_free($xml_parser);
                    
                    
    // putting in array
                    
    $news=array();
                    if (isset(
    $this->rss_channel["ITEMS"])) 
                    {
                        if (
    count($this->rss_channel["ITEMS"]) > 0
                            for(
    $i 0;$i count($this->rss_channel["ITEMS"]);$i++) $news[]=$this->rss_channel["ITEMS"][$i];
                    }
                    
    $c=0;
                    
                    foreach(
    $news as $key=>$val)
                    {
                        if(
    $c<$this->nr_news)
                        {
                            
    $template->assign_block_vars($this->feedname, array(
                            
    'LINK' => isset($val['LINK']) ? $val['LINK'] : '',
                            
    'TITLE' => isset($val['TITLE']) ? $val['TITLE'] : '',
                            
    'DESC' => isset($val['DESCRIPTION']) ? $val['DESCRIPTION'] : '')
                            );
                        }
                        
    $c++;
                    }
                }
        private static function 
    curl_string ($url,$user_agent='Mozilla 4.0'){

           
    $ch curl_init();

           
    curl_setopt ($chCURLOPT_URL$url);
           
    curl_setopt ($chCURLOPT_USERAGENT$user_agent);
           
    curl_setopt ($chCURLOPT_HEADER0);
           
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);
           
    curl_setopt ($chCURLOPT_FOLLOWLOCATION1);
           
    curl_setopt ($chCURLOPT_TIMEOUT2);
           
    $result curl_exec ($ch);
           
    curl_close($ch);
           return 
    $result;
      
            }
    }


    ?>
    then i get this error:

    SQL ERROR [ mysqli ]

    Lost connection to MySQL server during query [2013]

    SQL

    SELECT COUNT(post_id) as total FROM phpbb_posts WHERE post_time >= 1181765730

    BACKTRACE

    FILE: includes/db/mysqli.php
    LINE: 118
    CALL: dbal->sql_error()

    FILE: bb3portal/block/user_menu.php
    LINE: 27
    CALL: dbal_mysqli->sql_query()

    FILE: bb3portal.php
    LINE: 42
    CALL: include('bb3portal/block/user_menu.php')
    note the last line.. i put the call to the feedlist before the user_menu is called and user menu fails... if I put the feed anywhere.. the next file fails

    any ideas?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Lost connection to MySQL server during query [2013]

    Have a look at this: http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

    Are you sure the connection hasn't been closed by the time it reaches your code?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Lost connection to MySQL server during query [2013]

    As discussed earlier with you on MSN, it appears your connection is timing out. The reason for this is you were loading 12 feeds with every request made to the page; this meant your script was making 12 subsequent HTTP requests to load the feed data each time and caused the script to take a long time to execute.

    This is a very bad idea. I have no idea why it should stop working now (maybe the previous version of PHPbb used the standard MySql functions). In any case, should traffic begin to pick up on your site, the overhead of all these extra requests on the host will create a massive performance hit and bring the server to a grinding halt. If you use shared hosting this would undoubtedly contravene their fair use policy and you would probably have your account terminated.

    You therefore need to cache the results of the feeds and update the cache files at regular intervals (say 5mins). Ideally, you don't want to piggy back off a users request to update the feeds and if your host allows it, use a cron job to update all feeds.

    Sometimes cron jobs are not an option and you have no choice. I have made a small modification to the feed class so it uses cache files. It is limited to two updates per request and the updates are carried out in the class constructor. I recommend you use an output buffer and flush this before making the updates so the user is not left waiting for your feeds to come through (what if the feed host is down?).

    PHP Code:
    <?php

    // 'http://www.1up.com/rss?x=1'

    class StaticFxRssFeed {
        
    /**
         * Maximum Update Count
         *
         * Never update more than this number of feeds per request.
         */
        
    private static $max_update_count 2;

        
    /**
         * Update Count
         *
         * Number of feeds updated for this request.
         */
        
    private static $update_count 0;

        
    /**
         * Maximum Cache Age
         *
         * Maximum age of the cache file for feed before it is updated, in seconds.
         */
        
    private $max_age 300;

        
    /**
         * News
         *
         * Array containing news.
         */
        
    private $news = array();

        
    /**
         * Cache Directory
         *
         * Directory containing feed caches.
         */
        
    private $cache_dir 'cache/';

        
    /**
         * Update Feed
         *
         * Set to true to update this feed on this request. N.b: feed will only be updated if
         * the maximum number of updates for this request has not been exceeded.
         */
        
    private $update false;

        
    /**
         * Cache File
         *
         * Location of the feed cache file.
         */
        
    private $cache_file;

        private 
    $nr_news=8;
        private 
    $rss_channel = array();
        private 
    $currently_writing "";
        private 
    $main "";
        private 
    $item_counter 0;
        private 
    $template;
        private 
    $feedname;
        private 
    $url;
        
        function 
    __construct($feedname$url) {
            
    $this->feedname $feedname;
              
    $this->url $url;
            
    $this->cache_file $this->cache_dir $this->feedname;

            
    // see if a cache file exists for this feed (using feed name)
            
    if (file_exists($this->cache_file)) {
                
    // load the data
                
    $this->news unserialize(file_get_contents($this->cache_file));
            } else {
                
    $this->update true;
            }
        }

        function 
    __destruct()
        {
            
    // only load feeds when the class is destroyed. You may want to clear any output bufferes at this point.
            
            // check the following before atempting to update feed:
            // The update flag is set to true OR the cache file is older than max_age AND
            // the update count does not exceed the maximum updates per request.
            
    if (($this->update || ((@filemtime($this->cache_file) + $this->max_age) < time())) 
                    && (
    self::$update_count self::$max_update_count)) {
                
                
    $this->update_feed();
                ++
    self::$update_count;
            }
        }

        function 
    startElement($parser$name$attrs) {
               switch(
    $name) {
                   case 
    "RSS":
                   case 
    "RDF:RDF":
                   case 
    "ITEMS":
                       
    $this->currently_writing "";
                       break;
                   case 
    "CHANNEL":
                       
    $this->main "CHANNEL";
                       break;
                   case 
    "IMAGE":
                       
    $this->main "IMAGE";
                       
    $this->rss_channel["IMAGE"] = array();
                       break;
                   case 
    "ITEM":
                       
    $this->main "ITEMS";
                       break;
                   default:
                       
    $this->currently_writing $name;
                       break;
               }
        }
        
        function 
    endElement($parser$name) {
               
    $this->currently_writing "";
               if (
    $name == "ITEM") {
                   
    $this->item_counter++;
               }
        }
        
        function 
    characterData($parser$data) {
            if (
    $this->currently_writing != "") {
                switch(
    $this->main) {
                    case 
    "ITEMS":
                        if (isset(
    $this->rss_channel[$this->main][$this->item_counter][$this->currently_writing])) {
                            
    $this->rss_channel[$this->main][$this->item_counter][$this->currently_writing] .= $data;
                        } else {
                            
    //print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
                            
    $this->rss_channel[$this->main][$this->item_counter][$this->currently_writing] = $data;
                        }
                        break;
                }
            }
        }

        
    /**
         * Updates the Feed
         */
        
    private function update_feed()
        {
                
    $xml_parser xml_parser_create();
                
    xml_set_element_handler
                
    $xml_parser
                array(
    $this'startElement'), 
                array(
    $this'endElement'
                ); 
                
                
    xml_set_character_data_handler
                    
    $xml_parser
                    array(
    $this'characterData'
                ); 
                    
                
    $data self::curl_string($this->url);
                
    xml_parse($xml_parser,$data);
                
    xml_parser_free($xml_parser);
                    
                
    // putting in array
                
    $news=array();
                if (isset(
    $this->rss_channel["ITEMS"])) 
                {
                    if (
    count($this->rss_channel["ITEMS"]) > 0
                        for(
    $i 0;$i count($this->rss_channel["ITEMS"]);$i++) $news[]=$this->rss_channel["ITEMS"][$i];
                }
                
    $c=0;

                
    // save the data into the cache file
                
    file_put_contents($this->cache_fileserialize($news));
        }

        function 
    get_data(&$template
        {
                    
    $news = &$this->news// simply load the data from the news array. NEVER update it here
                    
    foreach($news as $key=>$val)
                    {
                        
    $template->assign_block_vars($this->feedname, array(
                            
    'LINK' => isset($val['LINK']) ? $val['LINK'] : '',
                            
    'TITLE' => isset($val['TITLE']) ? $val['TITLE'] : '',
                            
    'DESC' => isset($val['DESCRIPTION']) ? $val['DESCRIPTION'] : '')
                            );    
                    }
                }
        private static function 
    curl_string ($url,$user_agent='Mozilla 4.0'){

           
    $ch curl_init();

           
    curl_setopt ($chCURLOPT_URL$url);
           
    curl_setopt ($chCURLOPT_USERAGENT$user_agent);
           
    curl_setopt ($chCURLOPT_HEADER0);
           
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);
           
    curl_setopt ($chCURLOPT_FOLLOWLOCATION1);
           
    curl_setopt ($chCURLOPT_TIMEOUT10);
           
    $result curl_exec ($ch);
           
    curl_close($ch);
           return 
    $result;
      
            }
    }


    ?>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Lost connection to MySQL server during query [2013]

    YOU!.. Yes YOU ARE THE MAN!!

    Thank you Very Very Very much... working great!!!!!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

Posting Permissions

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



Click Here to Expand Forum to Full Width