+ Reply to Thread
Results 1 to 2 of 2

Thread: PHP SQL Output

  1. #1
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    PHP SQL Output

    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.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP SQL Output

    If you just want to dump the values, you can use something like

    PHP Code:

    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 by descalzo; 11-10-2009 at 07:48 PM.

+ Reply to Thread

Similar Threads

  1. some php sql help needed
    By daktarie in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 12-05-2007, 05:04 PM
  2. Important PHP Information
    By Bryon in forum News and Announcements
    Replies: 0
    Last Post: 11-21-2007, 02:08 PM
  3. PHP and SQL issues.
    By DarkDragonLord in forum Free Hosting
    Replies: 17
    Last Post: 03-29-2007, 10:05 PM
  4. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers