Jump to content

Using Classes In Php


bigjoe11a

Recommended Posts

What I'm trying to do is access a class to access a mysql database and do an query, The problem is that the class as it is now needs to get the settings from a config.php file and well that doesn't seem to work. if I add the config.php file to the login.php script. It tells me I have a 2nd session started and values are all ready set. So I had to remove it from the top part of my login script.Can some one please help. The Class is blow

public function verifyDatabase()  {    //Database Connection Data    mysql_connect($settings['db_host'], $settings['db_user'], $settings['db_pass']) or die(mysql_error());    mysql_select_db($settings['db_name']) or die(mysql_error());    $data = mysql_query("SELECT ID FROM ".$settings['db_prefix']."users WHERE user_name = '{$this->_username}' AND user_pass = '{$this->_passmd5}'");    if(mysql_num_rows($data))	  {	    list($this->_id) = @array_values(mysql_fetch_assoc($data));	    return true;	  }    else	  { return false; }  }

The log in php code

<?phpif(isset($_POST['login'])){  include('sources/class.login.php');  $login = new Login();  if($login->isLoggedIn()) {	  define('LOG_IN',true);	 //header('location: index.php');  }  else    $login->showErrors();}$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));?><form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>"><table> <tr><td>Username:</td><td><input type="text" name="username" /></td></tr> <tr><td>Password:</td><td><input type="password" name="password" /></td></tr></table><input type="hidden" name="token" value="<?php echo $token;?>" /><input type="submit" name="login" value="Log In" /></form>

Link to comment
Share on other sites

Consider including your configuration data earlier and with constants instead of variables. That way, you can just add

include 'config.php';

at the top of your main PHP file ("index.php" I'd assume) and then use the constants within your function. An alternative - IMHO better - approach is to use what's commonly called "dependency injection" i.e. pass the configuration data to the class/method/whatever that needs it. Since logging in is necessarily done against a database, you might inject the DB configuration at the Login() class constructor.

Link to comment
Share on other sites

Thanks, I tried that I still have the same problem. It only seems to be in the _DbConnect(); and I don't have a constructor. for this class. DataManager. I do how ever for the login class I do. a constructor for that. That's below and I still have the same problem, The class for the DataManager is below all so...

public function __construct()  {    $this->_errors = array();    $this->_login  = isset($_POST['login'])? 1 : 0;    $this->_access = 0;    $this->_token  = $_POST['token'];    $this->_id	   = 0;    $this->_username = ($this->_login)? $this->filter($_POST['username']) : $_SESSION[SESS_USER];    $this->_password = ($this->_login)? $this->filter($_POST['password']) : '';    $this->_passmd5  = ($this->_login)? md5($this->_password) : $_SESSION['password'];	      }

Class DataManager()

class DataManager{        	 private static function _DbConnect()    {	    	    mysql_connect($settings['db_host'],$settings['db_user'],$settings['db_pass']) or die(mysql_error());	    mysql_select_db($settings['db_name']) or die(mysql_error());    }        public static function GetTotoalUsers()    {	    self::_DbConnect();	    	    list($count) = $array_values(mysql_fetch_assoc(mysql_query("SELECT count(*) FROM ".$settings['db_prefix']."users")));	    return $count;    }        public static function GetProfile()    {	    	    self::_DbConnect();	    	    $profile = mysql_query("SELECT * from " . $settings['db_prefix'] . " profile where id=".SESS_USERID."LIMIT = 1");	    	    $row = mysql_fetch_assoc($profile);		    $list = $row;		    	    return $list;    }    }

Any way as soon as I try and connect to the mysql database from any one of these classes. I get the same error. The values $settings['db_host']; is not fount like the others. Can you give me an idea on what to do, Sorry. I never have done this before. This is all so new to me. Joe

Link to comment
Share on other sites

The problem is that the $settings variable does not exist inside the function. The three options are to either use a global variable, which is the least desirable, define the settings as constants instead of variables, or passing the $settings variable or individual values into the constructor. Since multiple methods in the class all use those values, it makes sense to pass them to the constructor.

Link to comment
Share on other sites

define the settings as constants instead of variables, Thanks, That's what I had to do and it's working, Maybe I should start using them more. Most of the time nothing else works.Thank YouJoe

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...