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);
?>
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, eg 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.
If you have a table such as
ID | COL1 | COL 2
-----------------
1 | A1 | A2
2 | B1 | B2
Use the following PHP code to display the whole table
<?php
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
foreach($db->query('SELECT * FROM table') as $row) {
echo $row['field1'].' '.$row['field2']; //etc...
}
?>
This code is take ad verbatim from the link I provided earlier (I am being particularly kind - the vast majority of people wouldn't give you a link and then when you clearly hadn't read it still give you the code...).
The query can be extended to something such as 'SELECT * FROM table WHERE ID=2'
. Of course, you can still put any kind of HTML in the PHP forloop
to display the data anyway you want
As already stated I wouldn't use mysqldump
but a select
query. Having said that you can add a --where
statement to mysqldump
as described on the MySQL Documentation
mysqldump -t -u [username] -p[password] [database] [table] --where=[statement]
Also, you are trying to save the .sql file in your document root, you need to check the permissions. However having the document root writable by the Apache user is not a good idea.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.