Re: Loss of connection handle object

From: Date: Fri, 02 Mar 2001 01:48:14 +0000
Subject: Re: Loss of connection handle object
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-42268@lists.php.net to get a copy of this message
Well, you already have code to PostgreSQLDataSource ... the other object just encapsulates it ... ok heres some more code: class AbstractDataSourceManager { var $dataSourceName; var $dataSource; var $error_handler; ... function open() { <make $dataSource object with connection params> if (is_Object($dataSource)) return $dataSource->open(); else return false; } function executeQuery( $queryString ) { return $this->dataSource->executeQuery($queryString); } } -------------------- Test Code: if ($HTTP_GET_VARS["testno"]==7) { $manualNameTest = new AbstractDataSourceManager("PostgreSQLDataSource","","","wwwuser","","rp2"); $result = $manualNameTest->executeQuery("select * from requirements;"); $db = new PostgreSQLDataSource('','','wwwuser','','template1'); $db->open(); echo $db->connection."<BR>"; echo $db->executeQuery("select * from issues;"); } ---------------------- Test Output: The connection in open() is :Resource id #1 The connection in executeQuery() is :'' Warning: Supplied argument is not a valid PostgreSQL link resource in /var/wwwroot/php/PostgreSQLDataSource.php on line 67 <this is the pg_pConnect line...> The connection in open() is :Resource id #2 Resource id #2 The connection in executeQuery() is :'Resource id #2' Object ---------------- So in other words, the one using the encapsulator doesnt work - it seems to lose $this->connection in PostgreSQLDataSource when its contained in AbstractDataSourceManager ! ... theres something screwball here - i mean - an object should be able to access its own properties in any scope at any time shouldnt it ? This is totally screwing me here ... AndrewH ----- Original Message ----- From: "Andrew Hill" <ahill@openlinksw.com> To: "Andrew Halliday" <andrew@recalldesign.com>; <php-general@lists.php.net> Sent: Friday, March 02, 2001 11:53 AM Subject: Re: [PHP] Loss of connection handle object > Perhaps if you showed us some code? > > Best regards, > Andrew > > > On 3/1/01 7:35 PM, "Andrew Halliday" <andrew@recalldesign.com> wrote: > > > open() is simply supposed to open the connection to the database and store > > it in $this->connection. > > As you can see - it gets the connection info from either the objects > > properties or from parameters passed to it. > > Can you see what Ive done wrong...if anything?!?! > > > > AndrewH > > > > ----- Original Message ----- > > From: "Andrew Hill" <ahill@openlinksw.com> > > To: "Andrew Halliday" <andrew@recalldesign.com>; <php-general@lists.php.net> > > Sent: Friday, March 02, 2001 11:13 AM > > Subject: Re: [PHP] Loss of connection handle object > > > > > >> Andrew, > >> > >> What is the open() function? > >> > >> Best regards, > >> Andrew > >> --------------------------------------- > >> Andrew Hill - OpenLink Software > >> Director Technology Evangelism > >> eBusiness Infrastructure Technology > >> http://www.openlinksw.com > >> > >> > >> > >> > >> On 3/1/01 7:27 PM, "Andrew Halliday" <andrew@recalldesign.com> wrote: > >> > >>> Okay - Im writing an object to wrap a database. > >>> The key problem is this: > >>> - it declares a connection var as an object property > >>> - the open() function opens the connection to the database and stores > >>> the handle in $this->connection > >>> - the executeQuery() method complains about not having a valid handle, > >>> because by then, somehow, $this->connection==null!!! > >>> > >>> Any ideas?-i swear its just something stupid ive missed ... > >>> > >>> HELP! > >>> > >>> Thanx in advance, > >>> AndrewH > >>> > >>> ------------------------------------------------------------- > >>> The code following this returns the following to the browser: > >>> ------------------------------------------------------------- > >>> The connection in open() is :Resource id #1 > >>> The connection in executeQuery() is :'' > >>> > >>> Warning: Supplied argument is not a valid PostgreSQL link resource in > >>> /var/wwwroot/php/PostgreSQLDataSource.php on line 67 > >>> > >>> > >>> ------------------------------------------------------------- > >>> > >>> > >>> class PostgreSQLDataSource > >>> { > >>> var $connection; > >>> var $lastResultSet; > >>> var $error_handler; > >>> > >>> var $host, $port, $username, $password, $database; > >>> > >>> function PostgreSQLDataSource($host, $port, $username, $password, > >>> $database) > >>> { > >>> global $error_handler; > >>> $this->error_handler = $error_handler; > >>> > >>> $this->host = $host; > >>> $this->port = $port; > >>> $this->username = $username; > >>> $this->password = $password; > >>> $this->database = $database; > >>> > >>> //Legacy behaviour: $this->open($host, $port, $username, $password, > >>> $database); > >>> } > >>> > >>> function open(/*variable arg list*/) > >>> { > >>> $connectionString = ""; > >>> > >>> if (func_num_args() == 5) > >>> { > >>> $this->host = func_get_arg(0); > >>> $this->port = func_get_arg(1); > >>> $this->username = func_get_arg(2); > >>> $this->password = func_get_arg(3); > >>> $this->database = func_get_arg(4); > >>> } > >>> > >>> if ($this->host) > >>> $connectionString .= " host=".$this->host; > >>> if ($this->port) > >>> $connectionString .= " port=".$this->port; > >>> if ($this->username) > >>> $connectionString .= " user=".$this->username; > >>> if ($this->password) > >>> $connectionString .= " password=".$this->password; > >>> if ($this->database) > >>> $connectionString .= " dbname=".$this->database; > >>> > >>> $this->connection = pg_Connect($connectionString); > >>> echo "The connection in open() is > >>> :".$this->connection."<BR>"; > >>> if ($this->connection == false) > >>> return false; > >>> else > >>> return true; > >>> } > >>> > >>> function close() > >>> { > >>> return pg_Close($this->connection); > >>> } > >>> > >>> function executeQuery($queryString) > >>> { > >>> echo "The connection in executeQuery() is :'".$this->connection."'<BR>"; > >>> $this->lastResultSet = pg_exec($this->connection,$queryString); > >>> return new Iterator($this); > >>> } > >>> > >>> /*PRIVATE FUNCTIONS:*/ > >>> function getRow($row) > >>> { > >>> return pg_fetch_array($this->lastResultSet,$row); > >>> } > >>> > >>> function getResultLength() > >>> { > >>> return count($this->lastResultSet); > >>> } > >>> } > >>> > >>> > >>> > >>> > >> > > >

« previous php.general (#42268) next »