I have a class called Loader that contains a function that loads the data from the config file into the array. Below is what the classes look like. Can your'll see any errors? Perhaps I am doing something wrong.
//The class below is saved as file loader.php
class ConfigLoader{
var $settings;
function loadConfigFile(){
$file_handle = fopen("config/config.cfg", "rb");
$i = 0;
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode('=', $line_of_text);
$settings[$i]=$parts[1];
$i = $i + 1;
}
fclose($file_handle);
return $settings;
}
}
//This file is saved as mysql.php
require_once("loader.php");
class MySQL extends ConfigLoader{
var $connection;
var $host;
var $name;
var $password;
var $database;
function MySQL(){
$settings2 = ConfigLoader::loadConfigFile();
//When I use it like this it does not work
//remove the comments to use
//$this->host = $settings[0];
//$this->name = $settings[1];
//$this->password = $settings[2];
//$this->database = $settings[3];
//When I use it like this it works perfectly
//remove the comments to use
//$this->host = "localhost";
//$this->name = "admin";
//$this->password = "nimda";
//$this->database = "MyDatabase";
}
function dbconnect(){
$connection = mysql_connect($this->host,$this->name,$this->password);
mysql_select_db($this->database,$connection);
}
function dbclose(){
mysql_close($this->connection);
}
function fetchRow($query){
$rows = mysql_fetch_row($query);
return $rows;
}
function fetchArray($query){
$array = mysql_fetch_array($query);
return $array;
}
function fetchNum($query){
$num = mysql_num_rows($query);
return $num;
}
function dbquery($sql){
$query = mysql_query($sql) or die(mysql_error());
return $query;
}
}
<?
//This is the file that I am using for testing
require_once("sql/mysql.php");
$DB = new MySQL();
$connection = $DB->dbconnect();
$myqry = "SELECT Username, Password FROM login";
$query = $DB->dbquery($myqry);
//fetch all rows
while($array = $DB->fetchArray($query)){
print('Username= '.$array['Username'].' and Password= '.$array['Password']);
}
$DB->dbclose();
?>