Skip to content

Instantly share code, notes, and snippets.

@ricardobrg
Forked from b4oshany/sql_import.php
Last active January 15, 2019 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ricardobrg/ab711a84e7ded6cc7a84448001530ea6 to your computer and use it in GitHub Desktop.
Save ricardobrg/ab711a84e7ded6cc7a84448001530ea6 to your computer and use it in GitHub Desktop.
WPDB SQL File import
<?php
/* NOTICE
* This script imports SQL commands "as is".
* Double check the SQL commands before using it
* and BACKUP YOUR DATABASE.
* It needs some improvement to use wpdb->prepare.
*/
class WPSQLImporter{
/**
* Loads an SQL stream into the WordPress database one command at a time.
*
* @params $sqlfile The file containing the mysql-dump data.
* @return boolean Returns true, if SQL was imported successfully.
* @throws Exception
*/
public static function importSQL($sqlfile){
//load WPDB global
global $wpdb;
// read file into array
$file = file($sqlfile);
// import file line by line
// and filter (remove) those lines, beginning with an sql comment token
$file = array_filter($file,
create_function('$line',
'return strpos(ltrim($line), "--") !== 0;'));
// and filter (remove) those lines, beginning with an sql notes token
$file = array_filter($file,
create_function('$line',
'return strpos(ltrim($line), "/*") !== 0;'));
$sql = "";
$del_num = false;
foreach($file as $line){
$query = trim($line);
try
{
$delimiter = is_int(strpos($query, "DELIMITER"));
if($delimiter || $del_num){
if($delimiter && !$del_num ){
$sql = "";
$sql = $query."; ";
echo "OK";
echo "<br/>";
echo "---";
echo "<br/>";
$del_num = true;
}else if($delimiter && $del_num){
$sql .= $query." ";
$del_num = false;
echo $sql;
echo "<br/>";
echo "do---do";
echo "<br/>";
$wpdb->query($sql);
$sql = "";
}else{
$sql .= $query."; ";
}
}else{
$delimiter = is_int(strpos($query, ";"));
if($delimiter){
$wpdb->query("$sql $query");
echo "$sql $query";
echo "<br/>";
echo "---";
echo "<br/>";
$sql = "";
}else{
$sql .= " $query";
}
}
}
catch (\Exception $e)
{
echo $e->getMessage() . "<br /> <p>The sql is: $query</p>";
}
}
}
}
?>
@alaksandarjesus
Copy link

alaksandarjesus commented Jan 15, 2019

Hi
The below code worked form me after i use export in phpmyadmin and reset the database.
Thank you very much for being inspiration of what i was looking for.
`
function myImportSql($sqlfile){
$file = file($sqlfile);
// import file line by line
// and filter (remove) those lines, beginning with an sql comment token
$file = array_filter($file,
create_function('$line',
'return strpos(ltrim($line), "--") !== 0;'));
// and filter (remove) those lines, beginning with an sql notes token
$file = array_filter($file,
create_function('$line',
'return strpos(ltrim($line), "/*") !== 0;'));

                global $wpdb;

                    $command = '';
                    foreach($file as $line){
                    $command .= trim($line);
                    if(empty($command)){
                        continue;
                    }
                    $is_last_character_delimiter = $command[(strlen($command)-1)] === ';';
                    if(!$is_last_character_delimiter){
                        continue;
                    }
                        $wpdb->query( $command );
                        if($wpdb->last_error){
                            echo "<br>";
                            echo "error". $wpdb->last_error;
                            echo "<br>";
                        }
                    $command = '';
                }
        }

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment