byCoder
byCoder

Reputation: 9184

How to change export to mysql to file in php?

Now i have such method:

 function exportFromTransbase($table_name) {
 $odbc_query = "SELECT * FROM " . $table_name;

 $data = odbc_exec($this->odbc_id, $odbc_query);
 odbc_longreadlen($data, 10485760);

 while($row = odbc_fetch_array($data))
 {

 foreach($row as $key => $value) {
 $keys[] = "`" . $key . "`";
 $values[] = "'" . mysql_real_escape_string($value) . "'";
 }


 $mysql_query = "INSERT INTO `" . strtolower(substr($table_name, 4)) . "` (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ")";

 mysql_query($mysql_query);
 set_time_limit(3600);
 unset($keys);
 unset($values);
 unset($row);
 }
    if ($mysql_query){
        print "Ýêñïîðò äàííûõ èç òàáëèöû " . $table_name . " çàâåðøåí!";
        //strtolower(substr($table_name, 4))
    }
 }

But it's very slow when importing to mysql. I decide to change this to export to file .sql, so that in future i can via terminal or phpmyadmin import that table. How to change to export to sql file my data?

Note! i'm converting from transbase to mysql

Upvotes: 0

Views: 1067

Answers (2)

symcbean
symcbean

Reputation: 48387

Instead of...

while($row = odbc_fetch_array($data))
{
   foreach($row as $key => $value) {
      $keys[] = "`" . $key . "`";
      $values[] = "'" . mysql_real_escape_string($value) . "'";
   }


   $mysql_query = "INSERT INTO `" . strtolower(substr($table_name, 4)) . "` (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ")";

    mysql_query($mysql_query);
    set_time_limit(3600);  // this should not be here
    unset($keys);          // this is redundant
    unset($values);        // and this
    unset($row);           // and this too
 }

Try:

$oufile=fopen("export.sql", 'w') || die("error writing file");

while($row = odbc_fetch_array($data))
{
   foreach($row as $key => $value) {
      $keys[] = "`" . $key . "`";
      $values[] = "'" . mysql_real_escape_string($value) . "'";
   }


   $mysql_query = "INSERT INTO `" . strtolower(substr($table_name, 4)) . "` (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ")";

   fputs($outfile, $mysql_query . ";\n";        
 }

However it'll be much faster if you....

$oufile=fopen("export.sql", 'w') || die("error writing file");
fputs($outfile, "ALTER TABLE `" . . strtolower(substr($table_name, 4)) . "` DISABLE KEYS;\n";

while($row = odbc_fetch_array($data))
{
   foreach($row as $key => $value) {
      $keys[] = "`" . $key . "`";
      $values[] = "'" . mysql_real_escape_string($value) . "'";
   }

   $head="INSERT DELAYED INTO `" . strtolower(substr($table_name, 4)) . "` (" . implode(",", $keys) . ") VALUES ";
   $row[]="(" . implode(",", $values) . ")";

   if (count($row)>100) {
      flush_ins($outfile, $head, $row);
      $row=array();
   }       
 }
 if (count($row)) flush_ins($outfile, $head, $row);
 fputs($outfile, "ALTER TABLE `" . . strtolower(substr($table_name, 4)) . "` ENABLE KEYS;\n";
 fclose($outfile);
...
function flush($rows, $head, $fh) 
 {
    fputs($fh, $head . implode("\n,", $rows) . ";\n");
 }

Upvotes: 0

jeffery_the_wind
jeffery_the_wind

Reputation: 18238

see this post:

Easy way to export a SQL table without access to the server or phpMyADMIN

it uses the select into outfile syntax. The docs for this syntax are here: http://dev.mysql.com/doc/refman/5.1/en/select-into.html

you would do something like this:

mysql_query('SELECT * INTO OUTFILE "/path/to/my_file/my_file.sql" from '.$table_name)

Then this .sql file will be on your server.

If you don't have permissions to run the select into outfile synatx. You can use the mysqldump utility, like this:

<?php
    exec('mysqldump db_name '.$table_name.' > my_file.sql');
?>

This will create an .sql file with the name indicated.

Upvotes: 0

Related Questions