mercredi 15 juin 2016

How execute SQL file and inserting data into DATABASE TABLES using PHP

I have a SQL file which i created from another database (named as test) on my localhost and now i want to insert this data into another database ( named as server_db) via PHP Script .

I tried and my PHP Script is working fine and creating the tables into server_db database. But values in those tables are not inserting ..... Please Help

My PHP Code is given below

<?php 
class Executer {
   public $path=""; 
   public function execute($path){
    // MySql connectivity
    $link = mysql_connect("localhost","root","");
    mysql_select_db("server_db");

    //file content
    $content = file_get_contents($path);

    //remove the comments
    $lines = explode("n",$content);
    $content = '';

    foreach($lines as $line){
        $line = trim($line);
        if( $line && !$this->startsWith($line,'--') ){
            $content .= $line . "n";
        }
    }
    //convert data into array of queries
    $content = explode(";", $content);
    //run the query
    $total = $sucess=0;
    foreach($content as $command){
        if(trim($command)){
            $success = (mysql_query($command)==false ? 0 : 1);
        }
    }
}
public function startsWith($string, $sym_com){
    $length = strlen($sym_com);
    return (substr($string, 0, $length) === $sym_com);
}   


} $path = "C:/xampp/htdocs/final/downloads/server_database_file.sql";
execute($path); 

Aucun commentaire:

Enregistrer un commentaire