applebiter Posted February 26, 2006 Share Posted February 26, 2006 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 More sharing options...
CheesierAngel Posted July 6, 2006 Share Posted July 6, 2006 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.