如何从php脚本中使用where子句导出mysql表的某些行? [英] How to export some rows of a mysql table with where clause from a php script?
问题描述
我有一个说要测试的mysql表,我想使用php脚本为id在10到100之间的行创建一个可导入的.sql文件.
I have a mysql table say test and I want to create a importable .sql file for rows where id is between 10 and 100 using php script.
我想创建一个名为test.sql的sql文件,该文件可以导入到mysql数据库中.
I want to create a sql file say test.sql which can be imported to mysql database.
我的代码:
$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);
这将创建一个test.sql文件,但是当我尝试导入时会给出错误#1064 我的脚本只创建带有行名称,表名称严格或插入查询的文件.
This create 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.
推荐答案
如注释中所述,您可以通过以下方式使用mysqldump.
As mentioned in the comments you can use mysqldump the following way.
mysqldump --user=... --password=... --host=... DB_NAME --where=<YOUR CLAUSE> > /path/to/output/file.sql
如果您希望将其保存在您的php文件中,则可以执行以下操作
If you want this to be in your php file you can do the following
exec('mysqldump --user=... --password=... --host=... DB_NAME --where=<YOUR CLAUSE> > /path/to/output/file.sql');
这篇关于如何从php脚本中使用where子句导出mysql表的某些行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!