Jump to content


Photo

OOP in PHP


  • Please log in to reply
1 reply to this topic

#1 applebiter

applebiter
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 26 February 2006 - 02:43 AM

I was wondering if anyone could help me figure out where I am screwing up! I'm trying to write a really simple class that gets a MySQL resource link AND selects the proper database. In a separate definitions file I call the constructor and populate the new object's fileds. Then, in the actual working file, I include the definitions file. The MySQL error tells me that the resource link is invalid, and I don't know why.

First, the class:

class DataConnection{
    var $hostname;
    var $username;
    var $password;
    var $database;
    var $datalink;
    
    function setHostname($pV){
        $this->hostname = $pV;
    }
    
    function getHostname(){
        return $this->hostname;
    }
    
    function setUsername($pV){
        $this->username = $pV;
    }
    
    function getUsername(){
        return $this->username;
    }
    
    function setPassword($pV){
        $this->password = $pV;
    }
    
    function getPassword(){
        return $this->password;
    }
    
    function setDatabase($pV){
        $this->database = $pV;
    }
    
    function getDatabase(){
        return $this->database;
    }
    
    function setDatalink($pV){
        $this->datalink = $pV;
    }
    
    function getDatalink(){
        return $this->datalink;
    }
    
    function DataConnection($pV1,$pV2,$pV3,$pV4){
        $this->hostname = $pV1;
        $this->username = $pV2;
        $this->password = $pV3;
        $this->database = $pV4;
        $this->datalink = mysql_pconnect($this->hostname,$this->username,$this->password);
        if(!$this->datalink){
            return 0;
        }
        else if(!mysql_select_db($this->database,$this->datalink)){
            return 0;
        }
        else return $this->datalink;
    }    
}

Then, the definitions file (values deleted):

include "DataConnection.php";

function getDatalink(){
    if($_SERVER['WINDIR']){
        $db_link = new DataConnection("","","","");
        return $db_link;
    }
    if(!$_SERVER['WINDIR']){
        $db_link = new DataConnection("","","","");
        return $db_link;
    }
}

Finally, part of the working document:

require_once('../inc/DataConnection.php');

session_start();

$query_myResult = "SELECT * FROM rickhomepage WHERE pageNumber = 1";
$myResult = mysql_query($query_myResult, $datalink) or die(mysql_error());
$row_myResult = mysql_fetch_assoc($myResult);
$totalRows_myResult = mysql_num_rows($myResult);

Any help would be appreciated!

#2 CheesierAngel

CheesierAngel
  • Members
  • PipPipPip
  • Advanced Member
  • 105 posts
  • LocationBelgium

Posted 06 July 2006 - 11:22 AM

You are using your class constructor for retrieving a variable calculated by some class functions. A class constructor is not designed for returning vars but for returning the classes address. If you create a new function inside your class that makes the connections, like you are doing now in your contructor you will retrieve the correct db_link.

class dataConnection {
   // ...

  // constructor
  function dataConnection() {
    // empty
  }

  function makeConnection($pV1, $pV2, $pV3, $pV4) {
    $this->hostname = $pV1;
    $this->username = $pV2;
    $this->password = $pV3;
    $this->database = $pV4;
    $this->datalink = mysql_pconnect($this->hostname,$this->username,$this->password);
    if(!$this->datalink){
      return 0;
    } elseif(!mysql_select_db($this->database,$this->datalink)){
      return 0;
    }
    return $this->datalink;
  }

}

function getDatalink() {
  $conn = new dataConnection();
  if($_SERVER['WINDIR']){
    return $conn->makeConnection("", "", "", "");
  }
  if(!$_SERVER['WINDIR']){
    return $conn->makeConnection("", "", "", "");
  }
}

Your last part can also be written easier:

$query_myResult = "SELECT COUNT(*) as count FROM rickhomepage WHERE pageNumber = 1";
$myResults = mysql_query($query_myResult, $datalink) or die(mysql_error());
$myResult = mysql_fetch_object($myResults);
$totalRows_myResult = $myResult->count;







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users