Jump to content

[SOLVED] Run SQL File In PHP


monkeypaw201

Recommended Posts

... All you had to do was ask  ;D

 

here is the first chunk, on upload.php

 

<?php
require_once("Includes/parse.class.php");
mysql_select_db($database_vamsys, $vamsys);
$parseObj = new parse("Templates/$zip_dir/install.sql");
$res = $parseObj->startParsing();
?>

 

on the parse.class.php:

 

<?php

class parse
{
   var $file;
   
   function parse($file)
   {      
      $this->setFile($file);
     $this->startParsing();
   }
   
   /** 
   * @purpose : Sets filename to be parsed
   * @params $file
   * @return none
   */
   function setFile($file)
   {
      $this->file = $file;
   }

   /**
   * @purpose : Gets filename to be parsed
   * @params none
   * @return filename
   */
   
   function getFile()
   {
      return $this->file;
   }
   
   /** 
   * @purpose : Parses SQL file
   * @params none
   * @return none
   */

   function startParsing()
   {

      $file = $this->getFile();   
     // Getting the SQL file content  
     $content = file_get_contents($file);
    
     // Processing the SQL file content    
     $file_content = explode("\n",$content);   
   
     $query = "";
  
    // Parsing the SQL file content    
    foreach($file_content as $sql_line)
    {  
       if(trim($sql_line) != "" && strpos($sql_line, "--") === false)
       {    
          $query .= $sql_line;
         // Checking whether the line is a valid statement
         if(preg_match("/(.*);/", $sql_line))
         {
            $query = substr($query, 0, strlen($query)-1); 
           //Executing the parsed string, returns the error code in failure
           $result = mysql_query($query)or die(mysql_error());
           $query = "";
         }
       }
    } //End of foreach
        
  return true; 
   } //End of function
    
} //End of class
?>

 

it works fine, however it seems to be repeating everything in the sql file twice?!

Link to comment
Share on other sites

I believe it's because you have a method named the same as your class, making it a constructor.  Your constructor automatically gets run when you create your object, which runs your setfile/startparsing methods, but then you turn around and run the method again here:

 

$res = $parseObj->startParsing();

Link to comment
Share on other sites

okay so now you renamed your class.  You make a new object, passing your file name. You then execute startparsing() ...and it's trying to getfile but you never passed it the filename because your parse method that was acting as a constructor did that before, and now it's not doing that.  So now you have to pass your argument over to it from inside your startparsing method.

Link to comment
Share on other sites

Okay, you renamed your class. So if I read your script right, what should work is, instead of doing this:

$parseObj = new parse("Templates/$zip_dir/install.sql");
$res = $parseObj->startParsing();

do this:

$parseObj = new parsed(); // you did rename it to parsed, right?
// $res = $parseObj->startParsing(); old method call
// call this instead, passing the filename to it
$res = $parseObj->parse("Templates/$zip_dir/install.sql"); 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.