如何把一个sql文件里的内容通过php代码执行

if (@$link= mysql_connect("$host","$name","$pwd")){
       $db_selected = mysql_select_db("$db", $link);
           if($db_selected){
               $sql="drop database ".$post['db'];
               mysql_query($sql);
           }
           $sql="create database ".$post['db'];
           mysql_query($sql);
           $file=file_get_contents('./assets/we3.sql');
           $arr=explode('-- ----------------------------',$file);
           $db_selected = mysql_select_db($post['db'], $link);
           for($i=0;$i<count($arr);$i++){
               if($i%2==0){
                   $a=explode(";",trim($arr[$i]));
                   array_pop($a);
                   foreach($a as $v){
                       mysql_query($v);
                   }
               }
           }
           $str="<?php
return [
   'class' => 'yii\db\Connection',
   'dsn' => 'mysql:host=".$post['dbhost'].";port=3306;dbname=".$post['db']."',
   'username' => '".$post['dbname']."',
   'password' => '".$post['dbpwd']."',
   'charset' => 'utf8',
   'tablePrefix' => 'we_',   //加入前缀名称we_
];";
           file_put_contents('../config/db.php',$str);
          $sql="insert into we_user (uname,upwd) VALUES ('$uname','$upwd')";
           mysql_query($sql);
       mysql_close($link);
       $counter_file       =   'assets/existence.php';//文件名及路径,在当前目录下新建aa.txt文件
       $fopen                     =   fopen($counter_file,'wb');//新建文件命令
       fputs($fopen,   'aaaaaa ');//向文件中写入内容;
       fclose($fopen);
       $this->redirect(array('/login/login'));
   }else{
       echo "<script>
                   if(alert('数据库账号或密码错误')){
                        location.href='index.php?r=install/two';
                   }else{
                   location.href='index.php?r=install/two';
                   }
       </script>";

   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值