Results 1 to 1 of 1

Thread: Creating a simple SQL script executor

  1. #1
    Moderator riderz's Avatar
    Join Date
    Mar 2009
    Location
    EMalahleni, South Africa
    Posts
    1,350
    Thanks
    108
    Thanked 391 Times in 128 Posts
    Rep Power
    6

    Default Creating a simple SQL script executor

    Sometimes if you want to install a new PHP CMS, blog, wiki, ... you first need to create the necessary database. The installation packages contains the sql file you need to execute, however if you don't have any web based management tool then it can take a bit more time. In this article we will create a simple PHP solution which can execute any sql script on your MySQL database.

    Step 1.

    The front end of our script is a simple HTML form where you can define the general database connection information like host-name, user-name and password. It is really simple as you can see here:

    Code:
     <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
      <table width="100%">
       <tr>
        <td>Hostname:</td>
        <td><input name="hostname" type="text" /></td>
       </tr>
       <tr>
        <td>Username:</td>
        <td><input name="username" type="text" /></td>
       </tr>
       <tr>
        <td>Password:</td>
        <td><input name="password" type="password" /></td>
       </tr>
       <tr>
        <td align="center" colspan="2">
         <input type="submit" name="submitBtn" value="Install" />
        </td>
       </tr>
      </table>  
     </form>
    Step 2.

    I suppose that you have uploaded the sql file to execute to your webserver. To make it more easy now we just hard code the file name in the code but of course you can extend the HTML form with an input field to get this information.



    If the form was submitted then we try to connect to the database. If it was success then we load the sql batch file content into a variable let's call it to $sqlFile. As next step we will explode this string into an array. We know that sql statements must be ended with a semicolon so we use it as parameter of the explode method.



    The result is an array where each array element is a standalone sql statement.

    The actual code look like this:

    Code:
    <?php
       $con = mysql_connect($host,$user,$pass);
       if ($con !== false){
         // Load and explode the sql file
         $f = fopen($sqlFileToExecute,"r+");
         $sqlFile = fread($f,filesize($sqlFileToExecute));
         $sqlArray = explode(';',$sqlFile);
    ?>
    Step 3.

    To execute the statement we just need to create a foreach loop and execute the statements step by step. In case of an error we save the error code and the error text as well and finish the execution. The code for this process is the following:

    Code:
    <?php
         //Process the sql file by statements
         foreach ($sqlArray as $stmt) {
           if (strlen($stmt)>3){
                $result = mysql_query($stmt);
                  if (!$result){
                     $sqlErrorCode = mysql_errno();
                     $sqlErrorText = mysql_error();
                     $sqlStmt      = $stmt;
                     break;
                  }
               }
          }
    ?>
    Step 4.

    As last step we need to inform the user if the installation was success or not. We can do this by checking the error variables. If it is not set then everything was fine else we print out the error as follows:

    Code:
    <?php
       if ($sqlErrorCode == 0){
          echo "<tr><td>Installation was finished succesfully!</td></tr>";
       } else {
          echo "<tr><td>An error occured during installation!</td></tr>";
          echo "<tr><td>Error code: $sqlErrorCode</td></tr>";
          echo "<tr><td>Error text: $sqlErrorText</td></tr>";
          echo "<tr><td>Statement:<br/> $sqlStmt</td></tr>";
       }
    ?>
    Attached Files Attached Files
    ________________
    Jacques
    [Only registered and activated users can see links. Click Here To Register...]
    [Only registered and activated users can see links. Click Here To Register...]
    [Only registered and activated users can see links. Click Here To Register...]
    __________________

    NEVER FORGET TO CLICK THE TANX BUTTON IF U LIKE WHAT IM SHARING OR HELPING WITH

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Creating a simple PHP forum
    By riderz in forum Tutorials
    Replies: 10
    Last Post: 30-01-13, 09:12
  2. Creating a simple password protection
    By riderz in forum Tutorials
    Replies: 0
    Last Post: 20-02-10, 19:22
  3. Creating a simple PHP guestbook
    By riderz in forum Tutorials
    Replies: 0
    Last Post: 06-10-09, 15:55
  4. Creating simple PHP contact form
    By riderz in forum Tutorials
    Replies: 0
    Last Post: 06-10-09, 15:53
  5. Simple Chat Script
    By Death22 in forum Scripts Forum
    Replies: 0
    Last Post: 22-11-07, 03:05

Bookmarks

Posting Permissions

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

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19