2011-08-12 10 views
4

作成するテーブルが約48個ある.sqlファイルを実行する必要があります。これは、コメントとSQLコマンドで構成され、 ";"で終わります。これらのSQLコマンドを単一の文字列にまとめて実行する方法はありますか?私は、PHPを使用してファイル全体を一度に実行する必要があることを意味します。私はmysql_query関数を使って行ごとにそのファイルを実行できます。しかし、私はphpmyadminと同じように一度にすべてを実行する必要があります。PHPを使って* .sqlファイルを実行

これを行う方法はありますか?私はこれをPHPを使って行う必要があります。誰も私を助けることができますか?あなたは

を探している

+2

[質問6976355](http://stackoverflow.com/questions/6976355/excute-a-mysql-script-from-within-a-php-スクリプト/ 6976390) – nobody

答えて

7

代替は、コマンドラインクライアントを使用することです:

mysql -u USERNAME -p DATABASENAME < FILE.SQL 

おそらくexecおよび/またはsystemでそれを使用することができますただし、-pの後にパスワードを入力する必要があります。上記のコマンドはあなたにそれを促します。

1

あなたは

function batch($file){ 
    $queries = explode(";", $file); 
    foreach($queries as $query){ 
     mysql_query($query); 
    } 
} 
0

クエリを爆発することができ、私は、入力として$fileを取り、mysqlデータベースに対してそれを実行します非常に汎用的な機能を書かれています。

以下のコードはcodeigniterの中に書かれています。どのフレームワークでも変更するのは非常に簡単です。このコードは可能な限り移植性が高く、多くの環境で動作します。それは、派手な文字列の解析や分割を行いません。代わりに、mysqlやPHPの組み込みメソッドを多用しています。

<?php 
//This function will take a given $file and execute it directly in php. 
//This code is for use within a codeigntier framework application 
//It tries three methods so it should almost allways work. 
//method 1: Directly via cli using mysql CLI interface. (Best choice) 
//method 2: use mysqli_multi_query 
//method 3: use PDO exec 
//It tries them in that order and checks to make sure they WILL work based on various requirements of those options 
public function execute_sql($file) 
{ 
    //1st method; directly via mysql 
    $mysql_paths = array(); 
    //use mysql location from `which` command. 
    $mysql = trim(`which mysql`); 
    if (is_executable($mysql)) 
    { 
     array_unshift($mysql_paths, $mysql); 
    } 
    //Default paths 
    $mysql_paths[] = '/Applications/MAMP/Library/bin/mysql'; //Mac Mamp 
    $mysql_paths[] = 'c:\xampp\mysql\bin\mysql.exe';//XAMPP 
    $mysql_paths[] = '/usr/bin/mysql'; //Linux 
    $mysql_paths[] = '/usr/local/mysql/bin/mysql'; //Mac 
    $mysql_paths[] = '/usr/local/bin/mysql'; //Linux 
    $mysql_paths[] = '/usr/mysql/bin/mysql'; //Linux 
    $database = escapeshellarg($this->db->database); 
    $db_hostname = escapeshellarg($this->db->hostname); 
    $db_username= escapeshellarg($this->db->username); 
    $db_password = escapeshellarg($this->db->password); 
    $file_to_execute = escapeshellarg($file); 
    foreach($mysql_paths as $mysql) 
    { 
     if (is_executable($mysql)) 
     { 
       $execute_command = "\"$mysql\" --host=$db_hostname --user=$db_username --password=$db_password $database < $file_to_execute"; 
       $status = false; 
       system($execute_command, $status); 
       return $status == 0; 
     } 
    } 

    if ($this->db->dbdriver == 'mysqli') 
    { 
     //2nd method; using mysqli 
     mysqli_multi_query($this->db->conn_id,file_get_contents($file)); 
     //Make sure this keeps php waiting for queries to be done 
     do{} while(mysqli_more_results($this->db->conn_id) && mysqli_next_result($this->db->conn_id)); 
     return TRUE; 
    } 

    //3rd Method Use PDO as command. See http://stackoverflow.com/a/6461110/627473 
    //Needs php 5.3, mysqlnd driver 
    $mysqlnd = function_exists('mysqli_fetch_all'); 

    if ($mysqlnd && version_compare(PHP_VERSION, '5.3.0') >= 0) 
    { 
     $database = $this->db->database; 
     $db_hostname = $this->db->hostname; 
     $db_username= $this->db->username; 
     $db_password = $this->db->password; 
     $dsn = "mysql:dbname=$database;host=$db_hostname"; 
     $db = new PDO($dsn, $db_username, $db_password); 
     $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0); 
     $sql = file_get_contents($file); 
     $db->exec($sql); 

     return TRUE; 

    } 

    return FALSE; 
} 

のGithubの要旨

https://gist.github.com/blasto333/d5d9079c78565c97119506e3c4f5ae3e

関連する問題