Jaco Jaco - 15 days ago 5
MySQL Question

Export/Import some data from database with PHP

I want to export and import from my database, including a WHERE clause, to export some data and not all (For example: Exporting all data tables where numberID = 25). So, It's possible to do that? And, If it is possible, how I could import those data? Removing first previous data?

I was trying to export them from my database, but it doesn't work: What I did wrong? I'm working with XAMPP. Thanks for all!

<?php

//ENTER THE RELEVANT INFO BELOW
$mysqlDatabaseName ='myDatabase';
$mysqlUserName ='myUser';
$mysqlPassword ='myPass';
$mysqlExportPath =$_SERVER['DOCUMENT_ROOT'] . '/backup.sql';
$mysqlHostName ='localhost';

$command='mysqldump --opt -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' > ' .$mysqlExportPath;
exec($command);

?>

Answer

Try not to use mysqldump (it is more like an administration/backup tool and to my knowledge can only export whole tables), but try a SELECT statement with a WHERE clause for reading your data. Export it in the file format you would like to, e.g. XML oder CSV. For importing the data again, use a DELETE statement for deleting the present data (also applying the WHERE clause) and use INSERT statements for each record in the input file.

Comments