tags:

views:

420

answers:

7

I need to do a dump of a table on a remote server, but I can't access the server directly. The only access I have is through PHP scripts.

Is there some way in which MySQL will return an

INSERT INTO `table_name` (`field1`, `field2`) VALUES ('a', 'b'), ('c', 'd')

statement, like what mysqldump will return?

I don't have access to phpMyAdmin, and I preferably don't want to use exec, system or passthru.

See this question for another export method

+3  A: 

1) can you run mysqldump from exec or passthru
2) take a look at this: http://www.php-mysql-tutorial.com/perform-mysql-backup-php.php

Unkwntech
A: 

If you can use php-scripts on the server i would recommend phpmyadmin. Then you can do this from the web-interface.

Espo
You beat me to the phpmyadmin recommendation!
Redbeard 0x0A
A: 

You should check out PHPMyAdmin, it is a php based MySQL administration tool. It supports backups and recovery for the database as well as a 'GUI' to the database server. It works very well.

Redbeard 0x0A
A: 

I'm pretty sure phpMyAdmin will do this for you.

David Thibault
A: 

This

select 'insert into table table_name (field1, field2) values'
       || table_name.field1 || ', ' || table_field2 || ');'
from   table_name

should get you started. Replace || with the string concatenation operator for your db flavour. If field1 or field2 are strings you will have to come up with some trick for quoting/escaping.

agnul
A: 

Here is one approach generating a lot of separate query statements. You can also use implode to more efficiently combine the strings, but this is easier to read for starters and derived from this you can come up with a million other approaches.

$results = mysql_query("SELECT * FROM table_name"); while($row = mysql_fetch_assoc($results)) {

$query = "INSERT INTO `table_name` ";
$fields = '(';
$values = '('; 

foreach($row as $field=>$value) {
    $fields .= "'".$field."',";
    $values .= "'".mysql_escape_string($value)."',";
}

//drop the last comma off
$fields = substr($fields,0,-1);
$values = substr($values,0,-1);

$query .= $fields . " VALUES " . $values;

//your final result
echo $query;

}

See if that gets you started

My problem with this solution is that it doesn't return an accurate INSERT statement. It will, for example, enclose integers in single quotes.
Jrgns
Is it returning integers in single quotes or with accent graves (`)?
Brian Warshaw
This line:$values .= "'".mysql_escape_string($value)."',";encloses ALL values in single quotes ('). Generally it isn't a problem, but it isn't as accurate as PHP's putcsv function would do it.
Jrgns
+1  A: 

This code snippit should work well:

http://snipplr.com/view/173/mysql-dump/

Jrgns