Re: extending pear db

From: Date: Tue, 28 Oct 2003 10:15:59 +0000
Subject: Re: extending pear db
References: 1 2  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-8641@lists.php.net to get a copy of this message
<plug> This is almost exactly what this does... http://pear.php.net/manual/en/package.database.db-dataobject.intro-purpose.php </plug> Regards Alan Ámon tamás wrote:
Hello, This two way are very nice but I want to reside in my own site. I say what is my problems, maybe every should be solved with the original DB classes. 1. If I send a query whith an INSERT I like to get the insert_id (like mysql_insert_id()) 2. I have two function insertForm($table, $f) and updateTable($table, $f, $id='id', $idval=-1) where $table the sql table name and $f is an associative array what is come from a form where the keys are the sql table column names values are the values :) In updateTable the $id is the name of unique identifier, $idval is the id value. (if idval -1 it comes from the $f array). I send the code for easier interesting. If there is an easy to use solution is PEAR I use it, but I didn't find it. there is the 2 function and the full class what is not working: ----------- class a_db extends DB_common { function a_db() { //echo('hello'); } function insertForm($table, $f) { global $db; $sql = 'INSERT INTO '.$table .'('; $s = array(); foreach($f as $key=>$val) { $val=preg_replace('/\'/','\\\'',$val); $k[] = $key; $v[] = "'".$val."'"; } $sql .= join(', ',$k).') VALUES ('.join(', ',$v).")"; //echo($sql); $this->query($sql); } function updateForm($table, $f, $id='id', $idval=-1) { if($idval == -1) { $idval=$f['id']; } $sql = "UPDATE $table SET "; $s = array(); foreach($f as $key=>$val) { if($val == 'on') { $val=1; } if($id != $key) { $val=preg_replace('/\'/','\\\'',$val); $s[] = $key.'=\''.$val.'\''; } } $sql .= join(', ',$s).' WHERE '.$id.'='.$idval; //echo($sql); $this->query($sql); } } -----------
-- Can you help out? Need Consulting Services or Know of a Job? http://www.akbkhome.com

« previous php.pear.general (#8641) next »