RE: [PEAR] extending pear db

From: Date: Mon, 27 Oct 2003 14:32:19 +0000
Subject: RE: [PEAR] extending pear db
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-8624@lists.php.net to get a copy of this message
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); } } ----------- -- Ámon Tamás <sajt@amon.hu>

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