note 68455 added to function.mysql-query

From: Date: Thu, 27 Jul 2006 15:03:23 +0000
Subject: note 68455 added to function.mysql-query
Groups: php.notes 
Request: Send a blank email to php-notes+get-115271@lists.php.net to get a copy of this message
in the first note the function doesn't work and the other function is pretty complicated. Here is the corrected version of the first one and a function for update. <?php function mysql_insert($table, $toAdd){ $fields = implode(array_keys($toAdd), ','); $values = "'".implode(array_values($toAdd), "','")."'"; # better $q = 'INSERT INTO '.$table.' ('.$fields.') VALUES ('.$values.')'; $res = mysql_query($q)OR die(mysql_error()); return true; //-- Example of usage //$tToAdd = array('id'=>3, 'name'=>'Yo', 'salary' => 5000); //insertIntoDB('myTable', $tToAdd) } function mysql_update($table, $update, $where){ $fields = array_keys($update); $values = array_values($update); $i=0; $query="UPDATE ".$table." SET "; while($fields[$i]){ if($i<0){$query.=", ";} $query.=$fields[$i]." = '".$values[$i]."'"; $i++; } $query.=" WHERE ".$where." LIMIT 1;"; mysql_query($query) or die(mysql_error()); return true; //Example // mysql_update('myTable', $anarray, "type = 'main'") } ?> ---- Server IP: 192.35.244.50 Probable Submitter: 80.121.124.153 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-query.php Edit -- http://master.php.net/note/edit/68455 Del: integrated -- http://master.php.net/note/delete/68455/integrated Del: useless -- http://master.php.net/note/delete/68455/useless Del: bad code -- http://master.php.net/note/delete/68455/bad+code Del: spam -- http://master.php.net/note/delete/68455/spam Del: non-english -- http://master.php.net/note/delete/68455/non-english Del: in docs -- http://master.php.net/note/delete/68455/in+docs Del: other reasons-- http://master.php.net/note/delete/68455 Reject -- http://master.php.net/note/reject/68455 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#115271) next »