[eluser]Unknown[/eluser]
Hi every:
I have created a small library for parse SQL files and execute all queries. I need to use CI DB class from inside this class but I'm getting this error:
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined variable: ci
Filename: libraries/SQLParse.php
Line Number: 67
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/SQLParse.php
Line Number: 67
This is the code I wrote:
Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class SQLParse {
var $file;
var $ci;
function SQLParse() {
$ci = & get_instance();
$ci->load->database();
}
function setFile($file) {
$this->file = $file;
}
function getFile() {
return $this->file;
}
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 = $ci->db->query($query) or die(mysql_error());
$query = "";
}
}
} //End of foreach
return true;
} //End of function
} //End of class
?>
What I'm doing wrong?
Cheers and thanks in advance