close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

Let's start a discussion about PHP versions

Thread began 10/12/2023 3:44 pm by Patrice | Last modified 8/10/2024 10:23 am by Patrice | 3276 views | 89 replies

PatriceWebAssist

Follow up on export mysql to csv: Per David Hagood's suggestion to check out AI, this is solved. It's not perfect yet as I haven't figured out how to have a page to show client interface but a link to the page does do the download correctly. I am guessing I can fix with enough fiddling.

Thanks David!

Using CHAT 3.5, I asked: Write website interface to download a mysql file to csv using php


<?php
// MySQL database connection parameters
$host = 'localhost';
$username = 'user';
$password = 'password';
$database = 'database';

// Connect to MySQL database
$mysqli = new mysqli($host, $username, $password, $database);

// Check connection
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
exit();
}

// SQL query to fetch data
$sql = "SELECT * table name";

// Execute query
$result = $mysqli->query($sql);

if (!$result) {
echo "Error: " . $mysqli->error;
exit();
}

// Create a file pointer
$fp = fopen('export.csv', 'w');

// Write column headers to CSV
$fields = $result->fetch_fields();
$column_headers = [];
foreach ($fields as $field) {
$column_headers[] = $field->name;
}
fputcsv($fp, $column_headers);

// Write data rows to CSV
while ($row = $result->fetch_assoc()) {
fputcsv($fp, $row);
}

// Close file pointer
fclose($fp);

// Free result set
$result->free();

// Close MySQL connection
$mysqli->close();

// Set headers for CSV file download
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
readfile('export.csv');
exit();
?>


HTML (doesn't get called into play)

<body>

Export
<form method="post" action="">
<input type="submit" name="export" value="Export to CSV">
</form>
</body>

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...