Advertisement
terorama

mySQL import

Oct 5th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.49 KB | None | 0 0
  1. <?php
  2.  
  3.    session_start();
  4.  
  5.  
  6. function clean_all() {
  7.  
  8.    ob_start();
  9.    $db = new mysqli ('localhost', '','', '');
  10.  
  11.    if (mysqli_connect_errno()) {
  12.       exit (mysqli_connect_error());
  13.    }
  14.  
  15.    $rez = $db->query('select table_name from information_schema.tables
  16.        where table_schema=\'dmkim1_wp4\'
  17.  
  18. ');
  19.    if (!$rez)
  20.       exit ($db->error);
  21.    
  22.    echo '<table><tr><td>';
  23.    while ($row = $rez->fetch_field()) {
  24.       echo '<td>'.$row->name.'</td>';
  25.       //echo '<pre>'.print_r($row,true).'</pre>';
  26.  
  27.    
  28.  
  29.    }
  30.    echo '</td></tr>';
  31.  
  32.  
  33.   while ($row=$rez->fetch_array(MYSQLI_NUM)) {
  34.    
  35.       echo '<tr>';
  36.       for ($i=0; $i<count($row); $i++) {
  37.          echo '<td>'.$row[$i].'</td>';
  38.       }
  39.  
  40.        if (!$db->query('delete from '.$row[0])) {
  41.          echo '<td>'.$db->error.'</td>';
  42.       }
  43.  
  44.       echo '</tr>';
  45.      
  46.   }
  47.  
  48.   echo '</table>';
  49.   $rez->free();
  50.  
  51.   ob_end_clean();
  52.  
  53. }
  54.  
  55. function draw_info() {
  56.  
  57.    $infsql = $_SESSION["arrs"][$_SESSION["step"]];
  58.    echo 'current step: '.$_SESSION["step"].'<br/><br/>';
  59.    echo 'current sql: '.$infsql.'<br/>';
  60.  
  61. }
  62.  
  63. function proc_err() {
  64.    $s =file_get_contents('errlog.html');
  65.    echo '<div style="font-family:courier new, monospace;">'.$s.'</div>';
  66. }
  67.  
  68. //-----------------------------------------------
  69. function proc_reqs() {
  70.  
  71.    $infsql = $_SESSION["arrs"][$_SESSION["step"]];
  72.  
  73.    $db = new mysqli('','','','');
  74.  
  75.    if (mysqli_connect_errno()) {
  76.       exit (mysqli_connect_error());
  77.    }
  78.  
  79.    if(!$db->query('SET NAMES utf8'))
  80.       exit('wtf');
  81.  
  82.    function clr($el) {
  83.  
  84.       return substr(ltrim($el),0,2)!='--';
  85.    }
  86.  
  87.    echo '<pre>'.print_r(array_filter(explode("\n", $infsql),'clr'),true).'</pre>';
  88.    $infsql = implode (' ', array_filter(explode("\n", $infsql),'clr'));
  89.  
  90.    echo '<br/>'.strlen(trim($infsql)).'<br/>';
  91.    if (strlen(trim($infsql))!=0) {
  92.    if (!$db->query($infsql)) {
  93.  
  94.       $fz= fopen('errlog.html','a');
  95.       fwrite($fz, $_SESSION["step"].'<br/><br>');
  96.       fwrite($fz,  $infsql.'<br/>'.$db->error);
  97.       fwrite($fz, '<br/>'.str_repeat('-',100).'<br/><br/>');
  98.       fclose($fz);
  99.      
  100.    
  101.    }}
  102.    $_SESSION["step"]++;
  103.    //echo $_SERVER["REQUEST_URI"];
  104.  
  105.    $s = <<<INF
  106.    
  107.       <script type="text/javascript">
  108.           window.onload = function() {
  109.             setTimeout('location.replace(location.href);',500);
  110.           }
  111.       </script>
  112.  
  113. INF;
  114.  
  115.    echo $s;
  116.    
  117.    //header('Location: '.$_SERVER["REQUEST_URI"].
  118. }
  119.  
  120. //-----------------------------------------------
  121.   function draw_pg() {
  122.  
  123.      $s = <<<INF
  124.      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  125.      <script type="text/javascript">
  126.  
  127.         window.onload = function() {
  128.            
  129.            wzz = window.open (location.protocol+'//'+location.hostname+location.pathname+location.search+'&proczz','wninf');
  130.            izz = setInterval ( function() {
  131.               frames["statusfr"].location.replace(location.href+'&infozz');
  132.               frames["errfr"].location.replace(location.href+'&errzz');
  133.  
  134.             }, 1000);
  135.         }
  136.      </script>
  137.  
  138.      <iframe name="statusfr" width="800" height="500">
  139.      </iframe>
  140.  
  141.      <iframe name="errfr" width="800" height="500">
  142.      </iframe>
  143.  
  144. INF;
  145.  
  146.    echo $s;
  147.  
  148. }
  149.  
  150.    function check_z() {
  151.       //echo '<pre>'.print_r($_SESSION, true).'</pre>';
  152.       $arrs = $_SESSION["arrs"];
  153.       //echo $arrs[0];
  154.  
  155.       if ((!isset($_GET["proczz"])) && (!isset($_GET["infozz"])) && (!isset($_GET["errzz"]))) {
  156.          
  157.          draw_pg();
  158.          return;
  159.  
  160.       }
  161.  
  162.       if (isset($_GET["infozz"])) {
  163.          draw_info();
  164.          return;
  165.          }
  166.  
  167.       if (isset($_GET["proczz"])) {
  168.          proc_reqs();
  169.          return;
  170.       }
  171.  
  172.      if (isset($_GET["errzz"])) {
  173.          proc_err();
  174.          return;
  175.       }
  176.  
  177.      
  178.    }
  179.    
  180.  
  181.    if (isset($_GET["gogo"])) {
  182.       check_z();
  183.       exit();
  184.    }
  185.  
  186.    $logf = fopen('errlog.html','w');
  187.    fclose($logf);
  188.    clean_all();
  189.  
  190.  
  191.    ob_start();
  192.    define ('SQFILE','upl/k09.hostenko.com.sql');
  193.  
  194.  
  195.    $s = file_get_contents(SQFILE);
  196.  
  197.    $arrs = explode(";\n",$s);
  198.  
  199.    $_SESSION["arrs"] = $arrs;
  200.    $_SESSION["step"] = 0;
  201.    session_commit();
  202.    header('Location: '.$_SERVER["PHP_SELF"].'?gogo');
  203.  
  204.    ob_end_flush();
  205.    exit();
  206.  
  207.  
  208. //------------------------ temp part for upload
  209.    
  210.    //---------------------------------
  211.    function proc_uploaded() {
  212.      
  213.      $uplfile = $_FILES;
  214.      echo '<pre>'.print_r($uplfile,true).'</pre>';
  215.      if ($uplfile["fname"]["error"]===0) {
  216.         if (!file_exists('upl')) {
  217.            mkdir('upl');
  218.         }
  219.          move_uploaded_file($uplfile["fname"]["tmp_name"], 'upl/'.$uplfile["fname"]["name"]);
  220.          
  221.      } else {
  222.         echo 'error uploading:'.$uplfile["fname"]["error"];
  223.      }
  224.  
  225.    
  226.  
  227. }
  228.  
  229.    if (!isset($_POST["submit"])) {
  230.  
  231.       $s = <<<INF
  232.  
  233.    <script type="text/javascript">
  234.       window.onload = function() {
  235.           var el = document.createElement('DIV');
  236.           el.innerHTML = 'location '+location.href;
  237.           document.body.appendChild(el);
  238.       }
  239.    </script>
  240.    <form action="{$_SERVER["PHP_SELF"]}" method="post" enctype="multipart/form-data">
  241.    <fieldset>
  242.    <legend>Upload sql file</legend>
  243.    <label for="fname">Choose file</label><input type="file" name="fname" />
  244.    <p><input type="submit" name="submit" value="upload" /></p>
  245.    </fieldset>
  246.    </form>
  247.  
  248. INF;
  249.  
  250.    echo $s;
  251.    
  252.    
  253.    } else {
  254.          proc_uploaded();
  255.    }
  256.  
  257.    
  258.  
  259. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement