Is it possible to convert an SQL database to a CSV file that can be read as a link in Javascript?

1 answer

Iv managed to write a function in php that at the moment converts my database into a csv file that is downloaded from the link.

$host      = "localhost"; $user      = "root"; $pass      = ""; $db_name   = "the-earth-museum"; $exp_table = "explorer_stories";  $mysqli = new mysqli($host, $user, $pass, $db_name); $mysqli->set_charset("utf8"); if (!$mysqli)     die("ERROR: Could not connect. " . mysqli_connect_error());  // Create and open new csv file $csv  = $exp_table . '.csv'; $file = fopen($csv, 'w');  // Get the table if (!$mysqli_result = mysqli_query($mysqli, "SELECT * FROM {$exp_table}"))     printf("Error: %s\n", $mysqli->error);  // Get column names  while ($column = mysqli_fetch_field($mysqli_result)) {     $column_names[] = $column->name; }  // Write column names in csv file if (!fputcsv($file, $column_names))     die('Can\'t write column names in csv file');  // Get table rows while ($row = mysqli_fetch_row($mysqli_result)) {     // Write table rows in csv files     if (!fputcsv($file, $row))         die('Can\'t write rows in csv file'); }  fclose($file);  echo "<a href= \"$csv\"Download\n >Download</a>"; 

However instead of the file being downloaded I need the link to be read in place of the url. So when the page is loaded its pulling the data from the link rather than first downloading the file.

var url =   "data/story-data.csv";    var template = {   content: "<h1>{title}</h1><a href='{image}'><img src='img/Biak_Thumbnail.jpg' style='padding-right: 20px;padding-left: 20px;'></a><br><p>Discover <a href='{info}'>the objects</a>, follow <a href='{story}'>their story</a>, explore connections with people and <a href='{senseop}'>{place}</a><br><br>(Plus an additional <a href='{info}'>resource</a> you might find interesting)"   }; 

Is it possible? or would be more suitable to write the same function in javascript and then theres no crossover?

Received answer to this question:
The best answer according to the author of the question:

You can convert it via sql query and then read that file directly.


and then read that file.

Make sure you generated the file dynamically if there data is more and it takes time, there could be issues of synchronization. Multiple users can be doing the same operation at the same time.