[HELP] Old Syntax in OOP PHP

Status
Not open for further replies.

Jo$h

Posting Freak
Jul 7, 2010
1,030
79
I have a question, as I am starting on the begginings of CreepCMS, which will eventually become a community project.

Anyways, I am working on my first ever class in OOP, which is of course, going to be connect_db.

So I was just wondering if in the "method" that I am placing in that class, would it work if I tried to use the old PHP syntax, so for example:

PHP:
function connect() {

var $con="mysql_connect('$server','$user','$pass')";

mysql_select_db("$data", $con);

}
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
You can't use $server, $user and $pass... Since those vars aren't global variables and aren't defined in the function. Nor is data.

I suggest making a file called config.php and put in it:
PHP:
$CONFIG = array();

$CONFIG['mysql']['host'] = 'localhost';
$CONFIG['mysql']['user'] = 'root';
$CONFIG['mysql']['pass'] = 'root';
$CONFIG['mysql']['db'] = 'database';

Then in your mysql class do something like:

PHP:
class mysql
{
  public function connect()
  {
      global $CONFIG;
      $con = mysql_connect($CONFIG['mysql']['host'], $CONFIG['mysql']['user'], $CONFIG['mysql']['pass']);
      mysql_select_db($CONFIG['mysql']['db'], $con);
  }
}

Then in another file.. (Maybe, global.php?)

PHP:
require_once 'config.php';
require_once 'inc/class.mysql.php';

$mysql = new mysql();

$mysql->connect();

Cheers!
 
Status
Not open for further replies.

Users who are viewing this thread

Top