Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export some rows of a MySQL table from a WHERE clause?

How to export some rows of a MySQL table with where clause from a PHP script?

I have a MySQL say test and I want to create a importable .sql file for rows where id are between 10 and 100, using PHP script.

I want to create a sql file say test.sql which can be imported to MySQL database.

Mycode:

$con=mysqli_connect("localhost", "root","","mydatabase");

$tableName  = 'test';
$backupFile = '/opt/lampp/htdocs/practices/phpTest/test.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName WHERE id BETWEEN 10 AND 500";

$result = mysqli_query($con,$query);

This creates a test.sql file, but when I try to import, it gives error #1064.
My script only creates a file with rows with columns name and table sturcute or insert query.

like image 469
ehp Avatar asked Mar 08 '13 05:03

ehp


2 Answers

In very simply way go to your phpMyAdmin select you database whose particular rows you want to export click on "SQL" (To Run SQL query/queries on database) Write sql query and execute it Like select * from test table limit 500 now what ever result come Just at the bottom see "Query results operations" just click on Export

All done :-)

like image 197
Abhinav bhardwaj Avatar answered Sep 20 '22 12:09

Abhinav bhardwaj


Mysql Shell command

mysqldump -u user -p password -h host Database Table --where="Condition"> /path/exportFile.sql

Example

mysqldump -u user -p 123456 -h host Database Student --where="age > 10"> /path/exportFile.sql
like image 30
Surojit Paul Avatar answered Sep 19 '22 12:09

Surojit Paul