PHP代码直接导入sql文件[执行sql文件、source SQL文件]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq285744011/article/details/82861829

直接使用下面的class Someone的方法execute_sql( 路径 )即可:

<?php

    class Someone {
        public function __construct () {
            
        }
        
        public function execute_sql($file) {
            /* Define variables here */
            $dbc = array(
                'hostname' => 'localhost',
                'username' => 'root',
                'password' => 'root',
                'database' => 'name of your database'
            );

            //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($dbc['database']);
            $db_hostname = escapeshellarg($dbc['hostname']);
            $db_username = escapeshellarg($dbc['username']);
            $db_password = escapeshellarg($dbc['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 (isset($this->db) && isset($this->db->dbdriver) && $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 = $dbc['database'];
                $db_hostname = $dbc['hostname'];
                $db_username = $dbc['username'];
                $db_password = $dbc['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;
        }
    }
    /* End of CLASS */
    
    
    
    $file = '/path/to/sql/file/to/source/from/xx.sql';
    $obj = new Someone();
    $obj->execute_sql($file);

更多参考:

https://stackoverflow.com/questions/8315467/source-a-mysql-file-through-php

https://github.com/kepes/php-migration

https://stackoverflow.com/questions/7038739/execute-a-sql-file-using-php/41404203#41404203

https://stackoverflow.com/questions/814586/calling-mysql-exe-using-php-exec-doesnt-work/814722#814722

猜你喜欢

转载自blog.csdn.net/qq285744011/article/details/82861829