Creating a simple SQL script executor

Started on the old forum in February 2010 · view the original in the archive · old links redirect here

No replies · 797 views · started 16 years ago · 1 min read

riderz Lead · 975 posts #1

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:

 <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:

<?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:

<?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:

<?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>";
   }
?>

Jacques jacques@gw-designs.co.za http://coding.biz.tm Come join and lets make it a place to learn all the noobies how to code


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

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close