Jump to content

Recommended Posts

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:

[code]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;
    }    
}[/code]

Then, the definitions file (values deleted):

[code]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;
    }
}[/code]

Finally, part of the working document:

[code]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);[/code]

Any help would be appreciated!
Link to comment
https://forums.phpfreaks.com/topic/3609-oop-in-php/
Share on other sites

  • 4 months later...
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.

[code]
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("", "", "", "");
  }
}
[/code]

Your last part can also be written easier:

[code]
$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;


[/code]
Link to comment
https://forums.phpfreaks.com/topic/3609-oop-in-php/#findComment-53795
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.