[Cs-content-commits] SF.net SVN: cs-content:[398] trunk/1.0
PHP Templating & Includes System
Brought to you by:
crazedsanity
From: <cra...@us...> - 2009-06-25 21:32:23
|
Revision: 398 http://cs-content.svn.sourceforge.net/cs-content/?rev=398&view=rev Author: crazedsanity Date: 2009-06-25 21:32:15 +0000 (Thu, 25 Jun 2009) Log Message: ----------- Add simple methods for querying & running inserts + fix mysql lastID(). /cs_phpDB.class.php: * run_query() [NEW]: -- runs a query and returns data in the requested array format. * run_insert() [NEW]: -- runs an insert & returns the newly-created ID. /db_types/cs_phpDB__mysql.class.php: * lastID(): -- use internal connectionID to avoid problems with multiple connections. -- fix comment (referred to "lastOID()") Modified Paths: -------------- trunk/1.0/cs_phpDB.class.php trunk/1.0/db_types/cs_phpDB__mysql.class.php Modified: trunk/1.0/cs_phpDB.class.php =================================================================== --- trunk/1.0/cs_phpDB.class.php 2009-06-25 19:01:25 UTC (rev 397) +++ trunk/1.0/cs_phpDB.class.php 2009-06-25 21:32:15 UTC (rev 398) @@ -77,6 +77,80 @@ }//end get_dbtype() //========================================================================= + + + //========================================================================= + /** + * Performs queries which require results. Passing $indexField returns a + * complex array indexed from that field; passing $valueField will change + * it to a name=>value formatted array. + * + * NOTE:: when using an index field, be sure it is guaranteed to be unique, + * i.e. it is a primary key! If duplicates are found, the database class + * will throw an exception! + */ + public function run_query($sql, $indexField=null, $valueField=null) { + + $retval = array(); + + //length must be 19 as that's about the shortest valid SQL: "select * from table" + if(strlen($sql) >= 19) { + $this->exec($sql); + + $numRows = $this->numRows(); + $dbError = $this->errorMsg(); + if($numRows > 0 && !strlen($dbError)) { + if(strlen($indexField) && (is_null($valueField) || !strlen($valueField))) { + //return a complex array based on a given field. + $retval = $this->farray_fieldnames($indexField, null, 0); + } + elseif(strlen($indexField) && strlen($valueField)) { + //return an array as name=>value pairs. + $retval = $this->farray_nvp($indexField, $valueField); + } + else { + $retval = $this->farray_fieldnames(); + } + } + elseif($numRows == 0 && !strlen($dbError)) { + $retval = false; + } + else { + $this->exception_handler(__METHOD__ .": no rows (". $numRows .") or dbError::: ". $dbError ."<BR>\nSQL::: ". $sql); + } + } + else { + $this->exception_handler(__METHOD__ .": invalid length SQL (". $sql .")"); + } + + return($retval); + }//end run_query() + //========================================================================= + + + + //========================================================================= + /** + * Handles performing the insert statement & returning the last inserted ID. + */ + protected function run_insert($sql) { + + $this->exec($sql); + + if($this->numAffected() == 1 && !strlen($this->errorMsg())) { + //retrieve the ID just created. + $retval = $this->lastID(); + } + else { + //something broke... + $this->exception_handler(__METHOD__ .": failed to insert, rows=(". $this->numRows .")... " + ."ERROR::: ". $this->errorMsg() ."\n -- SQL:::: ". $sql); + } + + return($retval); + }//end run_insert() + //========================================================================= + } // end class phpDB ?> Modified: trunk/1.0/db_types/cs_phpDB__mysql.class.php =================================================================== --- trunk/1.0/db_types/cs_phpDB__mysql.class.php 2009-06-25 19:01:25 UTC (rev 397) +++ trunk/1.0/db_types/cs_phpDB__mysql.class.php 2009-06-25 21:32:15 UTC (rev 398) @@ -692,9 +692,9 @@ * get last ID of last INSERT statement */ function lastID() { - $retval = mysql_insert_id(); + $retval = mysql_insert_id($this->connectionID); return($retval); - }//end lastOID() + }//end lastID() //========================================================================= This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |