PDA

View Full Version : Problem with mysql_select_db() and class variable


ensinger
10-16-2006, 01:36 AM
I am creating a class to work with a knowledge base database. the class only works if I hardcode the database or pass it into each function that works with the database. Below is a scaled down version of the class, any explanation as to why it fails when selecting the database would be appreciated.


class kb
{
function kb($server = "localhost", $database = "KnowledgeBase")
{
$this->$server = $server;
$this->$database = $database;
}

function ShowOptions()
{
$link = mysql_connect($this->$server)
or die("Could not connect : " . mysql_error());
mysql_select_db($this->$database, $link) or die("Could not select database");
}

var $server;
var $database;
}

I'm using php 4 still, what gives? Should I update php on my development pc? Is this a bug or am I doing something wrong?

Brandoe85
10-16-2006, 01:47 AM
Don't use the dollar sign when accessing with $this:
$this->$server = $server;
Use:
$this->server = $server;

Good luck;

ensinger
10-16-2006, 01:59 AM
Thanks! I am just getting back into web development and honestly I never did any OOP in php, something I took up with a C# application I've been working on.

ensinger
10-16-2006, 02:14 AM
That got it working and I can deal without a reply to this, but I am curious and the php manual does not cover working with class variables.

When debugging I tested:

$kb = new kb($server, $database);
echo $kb->$database;

and that displayed the correct value.

Any insight?

Brandoe85
10-16-2006, 03:09 AM
Take a look through here:
http://www.php.net/manual/en/language.oop.php

Hope that helps.

ensinger
10-16-2006, 03:48 AM
Should I update php on my development pc?

Good stuff, it is definately a good idea to update my chm... looks like it is covered now.