简体   繁体   中英

Run 2 sql query with 1 query for save query result as .csv format

In my web I've 2 page. 1) admin.php 2) csv.php. In admin.php page following query is showing data from db. In csv.php page I used same query to save data to .csv format but Can't save it.

I decided to run this same query in ONE QUERY. So that I can get the query result and can save it to csv format.

Questions:

1) How do i run this query to ONE query ?

2) Following query is showing data successfully. So how do i save it as .csv file ?

I search google for that and found many result which is showing how do I save data as .csv format with only one query. But you see that I've 2 while statement in my query then how do i save it as .csv format ? NO idea :(

Thanks and Looking for your help. :)
Note: I'm new learner about php and mysql.

$sqlagentdetails = "select * from users WHERE company_name != ''";
$rowresult = mysql_query($sqlagentdetails); 
while($row = mysql_fetch_array($rowresult, MYSQL_ASSOC))
{
    $pc1 = $row['pc1'];
    $pc2 = $row['pc2'];
    $pc3 = $row['pc3'];
    $pc4 = $row['pc4'];                      
    $emailAgent = $row['user_email'];                      
    $user_id = $row['id'];


$myQuery =  mysql_query("
 SELECT * 
   FROM user_property upr 
  WHERE (postcode = '$pc1' OR
         postcode = '$pc2' OR
         postcode = '$pc3' OR
         postcode = '$pc4') AND
         datediff(CURDATE(), upr.creation_date) <= 7 AND
         NOT EXISTS(SELECT ofr.property_id 
                      FROM offers ofr 
                     WHERE ofr.property_id = upr.property_id AND
                           ofr.agent_id IN(SELECT id 
                                             FROM users 
                                            WHERE company_name !=''
                                          )
                   )
ORDER BY property_id DESC");


    while($row = mysql_fetch_array($myQuery)){

    // more data are goes to here...     

    }
}        

1) How do i run this query to ONE query ?

You don't want it to run as one query. It's usually better to have lots of small simple queries instead of one complicated query. In fact I would suggest you update your code to have even more queries, for example the contents of the "not exists()" should not be done as a subquery, it should be a completely separate query to improve performance.

2) Following query is showing data successfully. So how do i save it as .csv file ?

There are two parts, first you need to send the correct HTTP headers to trigger a CSV download:

header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="export.csv";' );

Then just print out the data in CSV format:

while ($row = mysql_fetch_array($myQuery)) {
  $first = true;
  foreach ($row as $cell) {
    if ($first)
      $first = false;
    else
      print ',';

    print '"' . addslashes($cell) . '"';
  }
  print "\n";
}

Note: CSV is a bad format, and this will only work in some editions of Microsoft Excel. Depending where the user lives (eg: Europe) it might not work properly. For most editions of Excel the above will work however. There is no good solution except to avoid using CSV.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM