Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Jen

get key value using strpos

// description of your code here

foreach($check as $key=>$val) {
               if(strpos($val,$filter_value))
               {
                    $var = $key;
               }
          }

Creating tables and inserting data on database using a file

// Creating tables and inserting data on database using a file

$filename = "database.sql";

          $filesize       = filesize($filename);
          $file_position  = isset($HTTP_GET_VARS['pos']) ? $HTTP_GET_VARS['pos'] : 0;
          $errors         = isset($HTTP_GET_VARS['ignore_errors']) ? 0 : 1;

          if (!$fp = fopen($filename,'rb')) {
               echo "Unable to open file $filename";
          }
          
          $buffer = '';
          $inside_quote = 0;
          $quote_inside = '';
          $started_query = 0;

          $data_buffer = '';

          $last_char = "\n";

          // Sets file position indicator
          fseek($fp,$file_position);

          while ((!feof($fp) || strlen($buffer))) {
               do {
                    // Deals with the length of the buffer
                    if (!strlen($buffer)) {
                         $buffer .= fread ($fp,1024);
                    }

                    // Fiddle around with the buffers
                    $current_char = $buffer[0];
                    $buffer = substr($buffer, 1);


                    if ($started_query) {
                         $data_buffer .= $current_char;
                    }  elseif (preg_match("/[A-Za-z]/i",$current_char) && $last_char == "\n")  {
                         $started_query = 1;
                         $data_buffer = $current_char;
                    } else {
                         $last_char = $current_char;
                    }
               } while (!$started_query && (!feof($fp) || strlen($buffer)));


               if ($inside_quote && $current_char == $quote_inside && $last_char != '\\') {
                    // We were inside a quote but now we aren't so reset the flag and carry on
                    $inside_quote = 0;
               
               } elseif ($current_char == '\\' && $last_char == '\\')     {
                    $current_char = '';     
               
               } elseif (!$inside_quote && ($current_char == '"' || $current_char == '`' || $current_char == '\'')) {
                    // We have just entered a new quote
                    $inside_quote = 1;
                    $quote_inside = $current_char;
               
               } elseif (!$inside_quote && $current_char == ';') {

                    // edit database
                    $db_prefix = $boardname."_".$sel_domain."_phpbb_";
                    $data_buffer = str_replace("phpbb_", "$db_prefix", $data_buffer);
                    $data_buffer = str_replace("{domain}", "$sel_domain_name", $data_buffer);
                    $data_buffer = str_replace("{path}", "$sel_domain_path", $data_buffer);                         
                    $data_buffer = str_replace("{language}", "$language", $data_buffer);
                    $data_buffer = str_replace("{boardadmin}", "$boardadmin", $data_buffer);
                    $data_buffer = str_replace("{admin_pass_md5}", "$admin_pass_md5", $data_buffer);                         
                    $data_buffer = str_replace("{email}", "$email", $data_buffer);                         
                    $data_buffer = str_replace("{timing}", "$timing", $data_buffer);
                    $data_buffer = str_replace("{boardname}", "$boardname", $data_buffer);
                    $data_buffer = str_replace("{initial_forum}", "$initial_forum", $data_buffer);
                    $data_buffer = str_replace("{initial_forum_desc}", "$initial_forum_desc", $data_buffer);
                    $data_buffer = str_replace("{initial_post_subject}", "$initial_post_subject", $data_buffer);
                    $data_buffer = str_replace("{initial_post}", "$initial_post", $data_buffer);                              
               
                    // End of query so execute query, clear data buffer and advance counter
                    mysql_query($data_buffer);

                    $data_buffer = '';
                    $last_char = "\n";
                    $started_query = 0;
               }

               $last_char = $current_char;
          }


          $new_position = ftell($fp) - strlen($buffer) - strlen($data_buffer);

          fclose($fp);

Select all tables with a certain prefix in a database and remove them

// Select all tables with a certain prefix in a database and remove them

mysql_query("SHOW TABLES LIKE '[table_prefix_here]\_%'");

#Delete Data from old database
define("TABLE_PREFIX", [table_prefix]);
define("TABLE_PREFIX_LEN", strlen(TABLE_PREFIX));

$tables = array();
$r = mysql_query("SHOW TABLES");

while (($row = mysql_fetch_row($r)) !== FALSE) {
     if (TABLE_PREFIX == substr($row[0], 0, TABLE_PREFIX_LEN)) {
          $tables[] = $row[0];
          mysql_query("DROP TABLE $row[0];");
          }
     }