RE: [PHP] Help w/pg_connect, pg_open.

From: Date: Wed, 13 Dec 2000 14:50:14 +0000
Subject: RE: [PHP] Help w/pg_connect, pg_open.
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-30093@lists.php.net to get a copy of this message
Am all for functions and classes so I understand your point, and have also run into the problem it is apparent you are now referring to. I havn't placed the pg_connect in a function so not sure what problems that will cause... I placed the pg_connect in the header of each page that would require it then pass the connection as a global to each function (it doesn't automatically pass into functions like MySQL connections). I have had to globalize the link identifier in EACH and every function that utilizes the connection to the database... here is a short bastardized form: $connection=pg_connect(database stuff); #either pass the connection to the function doThis($my,$other,$vars); function doThis($my,$other,$vars) { global $connection; $result=pg_exec($connection,$query); } Again, unlike the MySQL support that PHP has built in, the PostgreSQL connection identifier does not seem to automatically pass into each function... got the same errors that you were getting. Assigning the $connection as a global within each function solved this issue. Hope that helps. Dave -----Original Message----- From: Soma Interesting [mailto:dfunct@telus.net] Sent: Tuesday, December 12, 2000 11:36 PM To: Dave VanAuken Subject: RE: [PHP] Help w/pg_connect, pg_open. At 10:08 PM 12/12/2000 -0500, you wrote: >I don't understabnd the steps you are going through... here is some basic >code, see if this helps you. if not, try posting csome actual code so we >can comment on it. (this is older pg code) I'm stumped so I'm going to take you up on your offer: I've done things slightly different that you have. I've made a set of functions for opening, closing and passing a sql statement into a single file which I require() for any scripts which need database access. This is so that in the future if I need to migrate to another database - it'll be easier. The next thing I did was to make a single function which opens a connection, issues the query and closes the connection returning the result identifier. This single function essentially combines all three of the other functions and is essentially a short cut means of issuing a query - it saves me typing three short lines. :) All this code works fine in other situations, but the one I'm in now. Here is the problem code, the set of database functions called by this code are afterwards. And my thanks for any input you can offer. if(!isset($lesson_list)){ // register the variable "$lesson_list" // get list of lessons (for now this is hard coded, but later it will grab lessons based on which course). $sql = "SELECT id,lesson_name FROM lessons ORDER BY order_num;"; $result = OnEd_dbquery($sql); if(pg_numrows ($result)){ for($i=0; $i < pg_numrows ($result); $i++){ $data = pg_fetch_array($result, $i); $lesson_list[] = array($data["id"], $data["lesson_name"]); } } } echo $result; if(!isset($page_list) && isset($lid)){ //register the variable $page_list. // get list of pages to push in $page_list array. $sql = "SELECT pages.id, pages.page_name FROM pages,lessons_pages WHERE lessons_pages.lid = " . $lid . "AND lessons_pages.pid = pages.id ORDER BY pages.order_num;"; unset($result); // THE SCRIPT BREAKS HERE // If I comment out the pg_numrows() and pg_fetch_array // that are in the section above - then the script will continue as normal. // It makes no sense to me. $result = OnEd_dbquery($sql); if(pg_numrows ($result)){ for($i=0; $i < pg_numrows ($result); $i++){ $data = pg_fetch_array($result, $i); $page_list[] = array($data["id"], $data["page_name"]); } } } echo $result; - - - - - - - DB FUNCTIONS - - - - - - - // define constants define ("DBHOST", "localhost"); define ("DBPORT", "5432"); define ("DBPASSWORD", "Nil9Five"); define ("DBUSER", "root"); define ("DB", "OnEd"); // function to open connection to PostgreSQL function OnEd_dbopen($user=DBUSER, $passwd=DBPASSWORD, $db=DB){ $connection = pg_connect("user=$user password=$passwd dbname=$db") or die (); return $connection; } // function for issuing sql statement to PostgreSQL // example value for $sql: INSERT INTO customer (name) VALUES ('test'); function OnEd_dbsql($connection, $sql){ $result=pg_exec($connection, $sql) or die (); return $result; } // code to close a PostgreSQL db connection function OnEd_dbclose($connection){ $close_result = pg_close ($connection) or die (); return $close_result; } function OnEd_dbquery($sql, $user=DBUSER, $passwd=DBPASSWORD, $db=DB){ $connection = OnEd_dbopen($user, $passwd, $db); $result = OnEd_dbsql($connection, $sql); OnEd_dbclose($connection); unset($connection); return $result; } function OnEd_rowid($result, $connection, $table, $id_column){ $oid = PG_GETLASTOID($result); $sql = "SELECT \"$id_column\" from \"$table\" WHERE oid = $oid"; $r = PG_EXEC($connection, $sql); $row_id = PG_FETCH_ARRAY($r, 0); return $row_id[$id_column]; } ?>

« previous php.general (#30093) next »