Re: Usage of BD.php inside a class
| From: | Markus Fischer | Date: | Sat, 14 Apr 2001 04:06:33 +0000 |
| Subject: | Re: Usage of BD.php inside a class | ||
| References: | 1 2 3 4 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-52@lists.php.net to get a copy of this message | ||
On Fri, Apr 13, 2001 at 09:43:01PM +0200, Luc Saint-Elie wrote :
> At 16:16 13/04/01 +0200, Markus Fischer wrote:
> >Hi Luc,
> > > I'm looking for the pear equivalent of mysql_num_rows($result)
> >
> > Just use $result->numRows(); .
>
> Thanks a lot for your quick answer Marcus.. but I discovered another problem
>
> The following is part of a class.. and php complains
> >Fatal error: Call to a member function on a non-object in backend.php on
> line 139
>
> //==================================
> function login ($username, $password) {
> if (!$username || !$password) {
> return $this->error[14];
> }
> else {
> if (!eregi("^[[:alnum:]_-]+$", $username)) {
> return $this->error[3];
> }
> if (!eregi("^[[:alnum:]_-]+$", $password)) {
> return $this->error[7];
> }
>
> $db = DB::connect(
> "mysql://$dbuser:$dbpass@$dbhost/$dbname" );
> $sql = "select id from login where username =
> '$username' and password = '$password'";
> >> THIS IS LINE 139 $query = $this->db->query($sql);
> $result =$this->db->numRows($query);
> $this->db->disconnect();
> [.....]
> //==================================
> Where did I screw up ?
If you have
class foobar {
function somefunc() {
$db = DB::connect( 'blabla');
Then note that '$db' is local known to 'somefunc()' _only_ ! In
this case $db != $this->db !
If you want to keep the handle known to the class, just assign it
initially to $this->db :
class myclass {
function makeConnection() {
$this->db = DB::connect( '...');
.
.
$this->db->query( $sql);
.
.
If you call disconnect() in the same function anyway, you won't need
the handle outside the function and can remove the $this->
reference anywhere.
cheers,
Markus
--
Markus Fischer, http://josefine.ben.tuwien.ac.at/~mfischer/
EMail: mfischer@josefine.ben.tuwien.ac.at
PGP Public Key: http://josefine.ben.tuwien.ac.at/~mfischer/C2272BD0.asc
PGP Fingerprint: D3B0 DD4F E12B F911 3CE1 C2B5 D674 B445 C227 2BD0