I'm very new in php, so I have a very studid problem. I try to write class that will help me with work with DB. Code here:
<?php
class DbHelper{
private $databaseURL;
private $databaseUName;
private $databasePWord;
private $databaseName;
private $nameOfDbWithWorkers;
private $connection;
function __construct($dbURL, $dbUserName, $dbPword, $dbName, $nameOfDbWithWorkers){
$this->databaseURL = $dbURL;
$this->databaseUName = $dbUserName;
$this->databasePWord = $dbPword;
$this->databaseName = $dbName;
$this->nameOfDbWithWorkers = $nameOfDbWithWorkers;
}
function setConnectionToDb(){
$connection = mysql_connect($this->databaseURL,$this->databaseUName,$this->databasePWord) OR DIE("can't connect to DB");
mysql_select_db($this->databaseName, $connection)or die ("Error while connecting to database");
}
function getUser($login, $pass){
$query = "SELECT type FROM $this->nameOfDbWithWorkers WHERE login = '$login' and password = '$pass';";
$queryResult = $this->getDataFromDbByQuery($query);
echo $queryResult;
if ((mysql_affected_rows($connection) == 1)){
$meta = mysql_fetch_assoc($queryResult);
if ($meta['type']=='admin'){
return 'admin';
}
if ($meta['type']=='user'){
return 'user';
}
else{
return 'nomatch';
}
}
else{
return 'nomatch';
}
}
function getDataFromDbByQuery($query){
$this->setConnectionToDb();
$result = mysql_query($query);
mysql_close($connection);
return $result;
}
}
?>
I invoke getUser
method, but have a problem - $connection = null
in getUser
method and in getDataFromDbByQuery
. Why so?