Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Old 07-07-2008, 08:01 PM Lost connection?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I have some code which works, but only in 1 of 2 possible ways. I'd like to use the multi_query function of MySQLi, but each time I do at this point in the code, the server replies with a MySQL 2006 error of having lost the database connection.

PHP Code:
      if (isset($_POST["data_module"])) {
        
//$queries_to_execute = '';
        
foreach ($_POST["data_module"] as $line_id=>$module_name) {
          if (
$module_name == "Delete") {
            
//$queries_to_execute .= "DELETE FROM {$db_table_prefix}data_file_contents WHERE data_file_id=".$line_id."; DELETE FROM {$db_table_prefix}data_file WHERE data_file_id=".$line_id." LIMIT 1;";
            
$database->query("DELETE FROM {$db_table_prefix}data_file_contents WHERE data_file_id=".$line_id."; DELETE FROM {$db_table_prefix}data_file WHERE data_file_id=".$line_id." LIMIT 1");
          } else if (
$module_name == "Reset") {
            
//$queries_to_execute .= "UPDATE {$db_table_prefix}data_file_contents SET posted='No' WHERE data_file_id=".$line_id.";";
            
$database->query("UPDATE {$db_table_prefix}data_file_contents SET posted='No' WHERE data_file_id=".$line_id);
          } else {
            
$data_from $_POST["data_from"][$line_id];
            if (
$data_from == ""$data_from 0;
            
$data_to $_POST["data_to"][$line_id];
            if (
$data_to == ""$data_to 0;

            
$data_display_name preg_replace('/[^a-z0-9 \-\_\&]/i','',$_POST["data_display_name"][$line_id]);

            if (
$data_from <= $data_to) {
              
//$queries_to_execute .= "UPDATE {$db_table_prefix}data_file SET display_name='".$data_display_name."', data_module='".$_POST["data_module"][$line_id]."', sort_order=".$_POST['sort_order'][$line_id].", data_from=".$data_from.", data_to=".$data_to.", exclusive=".((isset($_POST['data_solo'][$line_id]))?1:0)." WHERE data_file_id=".$line_id." LIMIT 1;";
              
$database->query("UPDATE {$db_table_prefix}data_file SET display_name='".$data_display_name."', data_module='".$_POST["data_module"][$line_id]."', sort_order=".$_POST['sort_order'][$line_id].", data_from=".$data_from.", data_to=".$data_to.", exclusive=".((isset($_POST['data_solo'][$line_id]))?1:0)." WHERE data_file_id=".$line_id." LIMIT 1");
            } else {
              
$DatabaseMessage .= "Content source {$line_id} must have <b>from</b> value &le; <b>to</b> value.<br />";
            }
          }
        }
        
/*if (strlen($queries_to_execute) > 0) {
          $database->multi_query($queries_to_execute,true);
        }*/
      

$database is an instance of the following class:
PHP Code:
class MySQLiExtended extends MySQLi {
  public function 
multi_query($query$clear_result_set=false) {
    
//Execute query and store results
    
$query_results parent::multi_query($query);
    
//Clear result cache -- useful for UPDATE or INSERT commands where the result set isn't needed
    
if ($clear_result_cache) {
      while (
$this->next_result()) {
        
$result $this->use_result();
        if (
$result instanceof mysqli_result) {
          
$result->free();
        }
      }
    }
    return 
$query_results;
  }

I'm trying to clear the results there -- perhaps that's where something's happening? I've used that code before without any problems.

In the code in the first box above, it works as shown. Change it to use the multi_query code and it fails with the error code. Where did I go wrong?
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
 
Register now for full access!
Old 07-07-2008, 08:12 PM Re: Lost connection?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Julie figured it out! I need to match my variable names. The second code segment ought to be:
PHP Code:
class MySQLiExtended extends MySQLi {
  public function 
multi_query($query$clear_result_set=false) {
    
//Execute query and store results
    
$query_results parent::multi_query($query);
    
//Clear result cache -- useful for UPDATE or INSERT commands where the result set isn't needed
    
if ($clear_result_set) {
      while (
$this->next_result()) {
        
$result $this->use_result();
        if (
$result instanceof mysqli_result) {
          
$result->free();
        }
      }
    }
    return 
$query_results;
  }

__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-07-2008, 08:44 PM Re: Lost connection?
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
What was killing the actual connection?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-07-2008, 11:14 PM Re: Lost connection?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
The var $clear_result_set was mistyped in the first example. That Julie is a smart one, she's a keeper
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-07-2008, 11:20 PM Re: Lost connection?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Quote:
Originally Posted by Learning Newbie View Post
What was killing the actual connection?
It appears that if you don't clear the result set after a multi_query and then execute another query, MySQL craps out and kills the connection before the second query is executed. One would think that a more useful error would be thrown!

Quote:
Originally Posted by mgraphic View Post
The var $clear_result_set was mistyped in the first example. That Julie is a smart one, she's a keeper
Yep. She's the other half of the company! A very bright individual.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Lost connection?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.16103 seconds with 12 queries