Chud37
Chud37

Reputation: 5007

emulating phpMyAdmin Export function

When you export a database from phpMyAdmin, it returns an SQL file with create table queries and all the table data and information currently stored.

I want to create a button that downloads this to a file for the user.

is this possible, if so how?

Upvotes: 1

Views: 590

Answers (1)

Fluffeh
Fluffeh

Reputation: 33512

You use a mime type header that will suggest the user downloads the file and output the "created on the fly" sql insert codes that will create the entire table.

header("Content-type: application/octet-stream");
header("Content-type: application/force-download");

You can should be able to use PDO's get column meta to get the column types for your initial make table SQL.

The output looks like this:

<?php
$select = $DB->query('SELECT COUNT(*) FROM fruit');
$meta = $select->getColumnMeta(0);
var_dump($meta);
?>

array(6) {
  ["native_type"]=>
  string(7) "integer"
  ["flags"]=>
  array(0) {
  }
  ["name"]=>
  string(8) "COUNT(*)"
  ["len"]=>
  int(-1)
  ["precision"]=>
  int(0)
  ["pdo_type"]=>
  int(2)
}

Then you query the database and use the data that is returned to generate an insert statement for each row that is returned from your initial query.

Upvotes: 1

Related Questions