Help with small mysql dump -data in php

Discussion in 'PHP' started by lowridertj, May 22, 2008.

  1. #1
    Looking to generate a backup of all tables using php without any data from the tables.

    How can this be done.
    A hot rush for me to get this going.

    Thanks,
    TJ
     
    lowridertj, May 22, 2008 IP
  2. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Is there any particular reason to do it via PHP?
     
    relixx, May 22, 2008 IP
  3. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #3
    Its a preferred language I like using.
     
    lowridertj, May 22, 2008 IP
  4. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    #4
    No complaints, lol.

    Well, the basic idea is as such:

    Get a list of all tables;
    loop through this list, selecting each table
    take the contents of the currently selected table and save it.

    How you want to save it is up to you.
     
    relixx, May 22, 2008 IP
  5. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #5
    I was more or less looking for a code to process it.
    The data does not need to be saved. Only the structure.

    TJ
     
    lowridertj, May 22, 2008 IP
  6. nihcer

    nihcer Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    Here's some code to fetch an then recreate some db structure:

    $dbServer = "localhost";
    $dbUserName = "root";
    $dbPassword = "";
    
    $dbName = "somedb";
      
    $conn = mysql_connect($dbServer, $dbUserName, $dbPassword);
    
    // select db information_schema
    mysql_select_db('information_schema', $conn);
    
    $tables = array();
    $sql="SELECT table_name FROM `TABLES` T where table_schema='$dbName';";
    $res = mysql_query($sql, $conn);
    while($t = mysql_fetch_assoc($res)) {
      $tables[] = $t['table_name'];
    }
    
    // select your db
    mysql_select_db($dbName, $conn);
    
    // fetch structure
    $db_structure = array();
    foreach($tables as $table_name) {
      $sql="SHOW CREATE TABLE " . $table_name ;
      $res = mysql_query($sql, $conn);
      $ts = mysql_fetch_assoc($res);
      $db_structure[] = $ts["Create Table"];
    }
    
    print_r($db_structure);
    PHP:
    It works for me.
     
    nihcer, May 22, 2008 IP
    lowridertj likes this.
  7. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #7
    thank you..
    Will test it out and rep you.
     
    lowridertj, May 22, 2008 IP
  8. kasapa

    kasapa Peon

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    for backup by using mysqldump

     
    kasapa, May 23, 2008 IP
  9. kasapa

    kasapa Peon

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    and this for restore

     
    kasapa, May 23, 2008 IP
    lowridertj likes this.