Page 1 of 1

Variable Scope Problem

Posted: Tue Apr 13, 2004 12:46 pm
by RadixDev
Rather than using PEAR I have created my own script to handle MySQL actions. I did this using OOP, and the class' called 'class_mysql'. I'm using this script to access data in the database in a user management (login) system. I created another sets of functions using OOP which has all the functions I need to manage users such as log-in, log-out, register, ban etc.

Problem comes here...

In the user management class I have a function called init(); which obviously is called before any other functions are run and in the function I load the MySQL script like so:

Code: Select all

<?php
class usrmanage {
  function init($dir="") {
    include($directory."config.php"); // MySQL Configuration
    include($directory."mysql.php"); // MySQL class
    $this->con=new class_mysql();
    $this->con->connect($directory); //connects to MySQL using config in config.php
    //$directory is used to identify the folder in which the files which will be included in the MySQL script exists.
  }
}
?>
Config.php:-

Code: Select all

<?php
$config['mysql']['host']="localhost";
$config['mysql']['username']="root";
$config['mysql']['password']="";
$config['mysql']['database']="radix";
$config['mysql']['prefix']="rdxls_";
?>
Now connect function in MySQL class:-

Code: Select all

<?php
class class_mysql() {
  var $root;
  function connect($dir="") {
    $this->root=$dir;
    include_once($dir."config.php");
    @mysql_connect($config['mysql']['host'], $config['mysql']['username'], $config['mysql']['password']);
    @mysql_select_db($config['mysql']['database']);
  }
}
?>
Error message from MySQL says that it execute sql queries later in the script because a database has not been selected. I'm using local MySQL server with no password so I think that's why the error is not caught in the connect() function... :?:

Using a debugger I found out that the variable $config suddenly dissapears once its in mysql class. Its there in the usrmanage class but then it dissappears. Is this a problem with my variable scopes?

Thanks for you help in advance, :wink:

RadixDev

Posted: Tue Apr 13, 2004 1:02 pm
by redmonkey
As you 'include' the config.php in the init function, I would guess that your 'include_once' call won't bring those vars in as the file has been included previously it won't be included again.

You could try replacing include_once for include within the connect function.

Posted: Tue Apr 13, 2004 4:05 pm
by RadixDev
No luck.. Any more ideas? :cry:

Posted: Tue Apr 13, 2004 5:32 pm
by redmonkey
I ran it though a quick test and replaced your mysql class with the following (threw in an echo to check it was getting there) and all seemed ok... (php 4.3.4 CLI)

Code: Select all

<?php
class class_mysql{
  var $root;
  function connect($dir="") {
    $this->root=$dir;
    include($dir."config.php");
    @mysql_connect($config['mysql']['host'], $config['mysql']['username'], $config['mysql']['password']);
    @mysql_select_db($config['mysql']['database']);
    
    echo $config['mysql']['host'];
  }
}
?>
You won't get any errors at connection time as you have suppressed the errors by way of the @ character, try removing the @ character and see if it throws any errors or add an 'or die'.

You could also try the following, the only difference with this is that you pass the $config vars when defining the new class...

Code: Select all

<?php
class usrmanage {
  function init($directory="") {
    include($directory."config.php"); // MySQL Configuration
    include($directory."mysql.php"); // MySQL class
    $this->con=new class_mysql($config['mysql']);
    $this->con->connect($directory); //connects to MySQL using config in config.php
    //$directory is used to identify the folder in which the files which will be included in the MySQL script exists.
  }
}
?>
And the mysql.php looks like....

Code: Select all

<?php
class class_mysql{
  var $root;
  var $params;

  function class_mysql($params){
    $this->params['host'] = $params['host'];
    $this->params['username'] = $params['username'];
    $this->params['password'] = $params['password'];
    $this->params['database'] = $params['database'];
  }
  
  function connect($dir="") {
    $this->root=$dir;
    @mysql_connect($this->params['host'], $this->params['username'], $this->params['password']) or die(mysql_error());
    @mysql_select_db($this->params['database']) or die(mysql_error());
  }
}
?>
Or you could define the database vars within the config.php e.g.

Code: Select all

<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'radix');
define('DB_PRFX', 'rdxls_');
?>
And connect like this....

Code: Select all

mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());

Posted: Tue Apr 13, 2004 5:34 pm
by RadixDev
I had the or die() bit but took it out for here sake...
I'll try out what you said now... Thanks!

Posted: Tue Apr 13, 2004 5:49 pm
by RadixDev
Mmmm interesting...
I had two versions of mysql class script. One which I use for my website and another for this new system thing. Guess what when I replaced the new system mysql class script with the website one and modified it a little it worked like a charm!!!???? Don't know what was wrong with it... Well thanx guys for your support!