How to export some rows of a mysql table with where clause from a php script?

后端 未结 4 1051
无人共我
无人共我 2021-01-04 08:45

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.

I want to create a sql file say t

4条回答
  •  臣服心动
    2021-01-04 09:04

    If you have exported data using OUTFILE then you have to import it using INFILE COMMAND

    $con=mysqli_connect("localhost", "root","","mydatabase");
    
    $tableName  = 'test';
    $backupFile = '/opt/lampp/htdocs/practices/phpTest/test.sql';
    $query      = "LOAD DATA INFILE '$backupFile' INTO TABLE $tableName";
    
    $result = mysqli_query($con,$query);
    

    OR you can make csv file using

     $con=mysqli_connect("localhost", "root","","mydatabase");
     $tableName  = 'test';
     $backupFile = '/opt/lampp/htdocs/practices/phpTest/test.sql';
     $query      = 'SELECT * INTO OUTFILE '."'$backupFile'".'
                    FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '.'\'"\'
                    LINES TERMINATED BY \'\n\'
                    FROM '.$tableName.' WHERE id BETWEEN 10 AND 500';
    
     $result = mysqli_query($con,$query);
    

    and then import this file.

    Hope this works...

提交回复
热议问题