PHP SQL Output

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
I am currently working on an extensive online application and I am looking to implement the javascript local database storage. However, I have run into quite a substantial problem; the javascript requires a SQL file to import data into the local database from the server database. I currently have the server database complete, but I cannot figure out a way of sending the sql table data from the server to the user's computer (It is not a security critical table, so it is fine to be stored in the user's browser and the table is relatively small). The idea of this is to reduce loading times when just doing a quick search.
The main issue here is the inability to use mysqldump or 'outfile' on the server to create a sql file that I could then read with javascript. Basically, what I am searching for is a solution that will let me parse a mysql table on the server into a sql file on the server, similar to the export function in phpmyadmin. The phpmyadmin export function is not a possible solution as the table updates with fresh data automatically once an hour.
If there is no viable solution, then it is likely I will have to create a completely custom script, so obviously it would be easier to check that there isn't any simpler ways of doing it first.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
If you just want to dump the values, you can use something like

PHP:
require_once( '/path/to/your/mysqlconnection.ini.php') ;
# the above connects to the DB and sets $link as the connection handle

$export_sql = "INSERT INTO `five` (`id`, `word`) VALUES " ;

$sql = "SELECT id, word FROM five " ;

$result = mysqli_query( $link , $sql ) ; 

if(! $result ){ 
   # OR HOWEVER YOU WANT TO HAND DB ERROR HERE
   echo "Query problem: " .  mysqli_error($link); 
   mysqli_close( $link );
   die();
 }

$first = true ;
while( $record = mysqli_fetch_row( $result )){
  if( ! $first ){ $export_sql .= ", " ; }
  $first = false;
  # id is a number, no quotes while word is text and requires quotes
  $export_sql .= "($record[0], '$record[1]')" ;
}

mysqli_close( $link );

echo $export_sql ;

This of course assumes you know the structure of the table you are going to dump.
If you want a more general method that will examine the table info and adjust itself to different number of columns and column names, that can be done.
 
Last edited:
Top