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

77 篇文章 2 订阅
22 篇文章 1 订阅

直接使用下面的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

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rudon滨海渔村

花的越多,赚得越多...

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值