bbc355f57b
Modification des chemins complet Attention à tester
77 lines
2.1 KiB
PHP
77 lines
2.1 KiB
PHP
<?
|
|
//error_reporting(E_ALL);
|
|
function mysql_trace($query, $res='') {
|
|
if (!$fp=fopen('mysql_insert.log', 'a'))
|
|
return false;
|
|
if (!fwrite($fp, date('Y/m/d - H:i:s') ." - $res : $query\n"))
|
|
return false;
|
|
if (!fclose($fp))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
/** Retourne la dernière valeur de l'auto-incrément ou 0 si pas d'auto-incrément
|
|
** Penser à bien tester la valeur !== false
|
|
**/
|
|
function mysql_insert($table, $toAdd, $debug=false){
|
|
$fields = implode(array_keys($toAdd), '`,`');
|
|
foreach (array_values($toAdd) as $key=>$array_values)
|
|
$tmp[$key]=addslashes($array_values);
|
|
|
|
$values = "'".implode(array_values($tmp), "','")."'"; # better
|
|
$values = str_replace("'NULL'", 'NULL', $values);
|
|
$query = 'INSERT INTO `'.$table.'` (`'.$fields.'`) VALUES ('.$values.');';
|
|
|
|
$res = mysql_query($query);// OR die(mysql_error());
|
|
if ($res!==false)
|
|
{
|
|
if (mysql_insert_id()>0)
|
|
$res=mysql_insert_id();
|
|
else
|
|
$res=true;
|
|
}
|
|
if ($debug) mysql_trace($query, $res);
|
|
|
|
return $res;
|
|
//-- Example of usage
|
|
//$tToAdd = array('id'=>3, 'name'=>'Yo', 'salary' => 5000);
|
|
//insertIntoDB('myTable', $tToAdd)
|
|
}
|
|
|
|
function mysql_update($table, $update, $where, $debug=false){
|
|
$fields = array_keys($update);
|
|
$values = array_values($update);
|
|
$i=0;
|
|
$query='UPDATE `'.$table.'` SET ';
|
|
while($fields[$i]){
|
|
if($i>0){$query.=', ';}
|
|
$query.=' `'.$fields[$i]."`='".addslashes($values[$i])."'";
|
|
$i++;
|
|
}
|
|
$query = str_replace("'NULL'", 'NULL', $query);
|
|
$query.=' WHERE '.$where.' LIMIT 1;';
|
|
if ($debug) mysql_trace($query, mysql_errno());
|
|
mysql_query($query);// or die(mysql_error());
|
|
return true;
|
|
|
|
//Example
|
|
// mysql_update('myTable', $anarray, "type = 'main'")
|
|
|
|
}
|
|
|
|
function mysql_select($table, $fields, $where, $debug=false, $assoc=MYSQL_BOTH) {
|
|
$query="SELECT $fields FROM $table WHERE $where;";
|
|
// if ($debug) mysql_trace($query, count($tab));
|
|
$result=mysql_query($query) or die(mysql_error());
|
|
$tab=array();
|
|
while ($ligne = mysql_fetch_array($result, $assoc))
|
|
$tab[]=$ligne;
|
|
|
|
mysql_free_result($result);
|
|
if ($debug) mysql_trace($query, count($tab));
|
|
// echo $query;
|
|
return $tab;
|
|
}
|
|
|
|
?>
|