note 68455 deleted from function.mysql-query by philip
| From: | philip@php.net | Date: | Fri, 04 Sep 2015 19:21:13 +0000 |
| Subject: | note 68455 deleted from function.mysql-query by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-203650@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster at vs2055067 dot vserver dot de
----
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'")
}
?>