Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    51
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Script to Create a MySQL Database

    I was hoping someone could point me in the right direction. I've made a PHP script that relies on a MySQL database, and I wanted to also make a script to create the required database for my PHP script for when I give a copy of my script to someone else. (Basically, so they can run the script to create the database, and then be able to use the PHP script that I've given them).

    SO, what language/tool do I use to make a database creation script? Are there security conflicts that I'm going to run into? Because the script will be creating a MySQL database on a web hosting account...
    Last edited by hunter1611; 12-25-2012 at 04:32 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,333 Times in 4,299 Posts
    Do you want to clone the existing database AND ALL ITS CONTENTS and have the other person install that?

    Or do you want to just clone the existing tables but with no data in any tables?

    Or some mix of the two?

    The first option--some database and all its contents--is actually the easiest. MySQL provides a command line tool that does this:
    Code:
    mysqldump -uUSERNAME -pPASSWORD nameofdatabase > anyNameYouChoose.sql
    Then, from the command line, it just as easy to restore (or re-create) that database:
    Code:
    mysql -uUSERNAME -pPASSWORD < anyNameYouChoose.sql
    Again, these are issued from the command line. From the LINUX command line or from at DOS window command line on Windows. The name of the dump file doesn't have to end in ".sql" but it's traditional to do so.

    ********

    If you want a mix, some tables with data and some without, the easiest way might be to dump the entire database, import (re-create) it on another machine, issue TRUNCATE TABLE xxx commands to clear the data out of the tables that are to be blank, and then re-dump from that second machine.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    hunter1611 (12-26-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,333 Times in 4,299 Posts
    If the web hosting account doesn't have access to the MySQL command line tools, then *probably* you could just use mysqlphpadmin to import the same ".sql" file.

    If not it would be trivial to create your own PHP program to do so: It would simply read one line at a time from the ".sql" text file and issue that command to the MySQL DB.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Jan 2011
    Posts
    51
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Hmmm...OK. Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •