function array_merge_sub(&$array1, &$array2) { $res = $array2; if (is_array($array1)) { foreach ($array1 as $key => $value) { if (!isset($res[$key])) { $res[$key] =& $array1[$key]; } else { if (is_array($res[$key]) && is_array($value)) { $res[$key] = array_merge_sub($array1[$key], $res[$key]); } } } } return $res; }
/** * @param array $params = array('table'=>string, 'fields'=>array(), 'safe_vals'=>int 0/1); * @param bool $replace (use REPLACE INTO) * @return int - insert_id */ public function Insert($params = array(), $replace = false) { $this->checkConnection(); //$params=array('table'=>string, 'fields'=>array(), 'safe_vals'=>int 0/1); $_def_params = array('safe_vals' => 1); $params = array_merge_sub($_def_params, $params); $fields = ''; $values = ''; $qry = ''; foreach ($params['fields'] as $field => $value) { $qry .= (strlen($qry) > 0 ? ', ' : ' ') . '`' . $field . "`=" . ($params['safe_vals'] == 1 ? "'" . mysql_real_escape_string($value) . "'" : $value); } $qry = ($replace ? 'REPLACE' : 'INSERT') . ' INTO `' . $params['table'] . '` set ' . $qry; if (!mysql_query($qry, $this->_handle)) { $this->errmess(mysql_error($this->_handle) . "<br>{$qry}<br>"); return 0; } $this->saveToLog($qry); return mysql_insert_id($this->_handle); }