Re: Class variables and scope
| From: | Michael Davey | Date: | Fri, 31 May 2002 15:31:00 +0000 |
| Subject: | Re: Class variables and scope | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-100106@lists.php.net to get a copy of this message | ||
I am doing that - sorry, should have given you a bit more code...
<?
class myClass
{
var $conn;
var $sth;
// etc...
function myClass ($db)
{
$host = "localhost";
$user = "user_name";
$password = "password";
$this->conn = mysql_connect ($host, $user, $pwd)
or die ("Unable to connect to database");
$sel = mysql_select_db ($db, $this->conn);
if (!$sel) die ("Unable to select database");
return true;
}
function doSomething ()
{
$sql = "select * from table";
$this->sth = mysql_query ($sql, $this->conn);
// do something with query...
}
}
?>
Is there something inherently static about msql connections?
Mikey
"Stuart Dallas" <stuart@sharedserver.net> wrote in message
news:165163457549.20020531161630@sharedserver.net...
> On Friday, May 31, 2002 at 4:11:42 PM, you wrote:
> > When I try to work with the first instance of my class, I get an error
which
> > I finally tracked down to it trying to run it's SQL queries against the
> > database defined in $c2. Now, I was under the impression that class
> > variables were private to each instance, however, this seems to be
acting
> > like a static var.
>
> You need to specify $this->conn in each mysql_* function you call
otherwise it
> will be called against the last connection made.
>
> --
> Stuart
>