2011-08-12 109 views
4

我需要執行一個.sql文件,它有大約48個表格來創建。它由註釋和以「;」結尾的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

回答

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