"Errore nella connessione a $DB[host]","nome"=>"query","gruppo"=>"INDI")), E_USER_WARNING); return FALSE; } else if (!mysql_select_db($database, $link)) { trigger_error("Errore selezione del database $database", E_USER_ERROR); //$GLOBALS['CONF']['force_no_redirect'] = "SI"; d_err("Errore nella selezione del database $database","mysql_select_db","DATAB"); //sleep(3); //apetto un po' //trigger_error(serialize(array("errore"=>"Errore nella selezione del database $DB[database]","nome"=>"query","gruppo"=>"INDI")), E_USER_WARNING); if($no_link) mysql_close($link); return FALSE; } else { //imposto globale if(!$no_link) $DB["link"] = $link; d_info("Collegato al DB: $database","connect","DATAB"); if(!$databaseCharset && $GLOBALS['CONF']['mcharset'] == "UTF-8"){ $databaseCharset = 'utf8'; } //l'altro database potrebbe avere un charset diverso da quello del sito (intercultura / intercultura_areavolontari) if($databaseCharset) { //mysql_query("SET character_set_connection=utf8",$link); //mysql_query("SET NAMES 'utf8'",$link); /* https://dev.mysql.com/doc/refman/5.7/en/charset-connection.html A SET CHARACTER SET charset_name statement is equivalent to these three statements: SET character_set_client = charset_name; SET character_set_results = charset_name; SET collation_connection = @@collation_database;//questo ci rende dipendendenti dalla configurazione del db che potrebbe differire online / locale mysql_query("SET CHARACTER SET '$databaseCharset'", $link); */ if($databaseCharset != 'utf8') trigger_error("controllare init del database con $databaseCharset (esiste collation $databaseCharset_general_ci?)"); mysql_query("SET character_set_client = ".$databaseCharset, $link); mysql_query("SET character_set_results = ".$databaseCharset, $link); //questi due sovrascrivono quelli della configurazione del database, cosė se i database online / locale hanno parametri diversi non ci influenza mysql_query("SET collation_connection = ".$databaseCharset."_general_ci", $link); mysql_query("SET character_set_connection = ".$databaseCharset, $link); // print_r(mysql_result(mysql_query("SELECT @@character_set_connection;", $link), 0));die; } } } //nocache if($_GET['no_cache'] || $_GET['no_sql_cache']) { $query = str_replace("SELECT","SELECT SQL_NO_CACHE",$query); } //risultato $result = mysql_query($query, $link); if (!$result) { $err = $database.":".mysql_errno($link) . ": " . mysql_error($link) . "\n$query\n"; $DB["error"][] = array($query,$database,mysql_errno($link),mysql_error($link)); //CARLO 17/5/10: aggiunto perchč altrimenti non si riceve NESSUN errore delle query online // if(mysql_errno($link) != 1062){//escludo i "Duplicate entry" CARLO 13/11/2012: non li escludo pių, aggiungero delle @ // if($_GET['dbg'] && $GLOBALS["CONF"]['dove_sono'] == 'rem' && error_reporting()){ // echo mysql_errno($link) . ": " . mysql_error($link) . '
debug_backtrace:
'; // debug_print_backtrace(); // echo '
'; // } d_err($err,"query","DATAB"); //trigger_error(serialize(array("errore"=>$err,"nome"=>"query","gruppo"=>"INDI")), E_USER_WARNING); // } if($no_link) mysql_close($link); return FALSE; } else { //calcolo tempo //24/03/2016 cecca e carlo, con script che fanno molte query (vedi I:\SITI\toysforyou2013\_pagine\export_libroco.inc.php) // utilizzava troppa memoria ed usciva il fatal quindi abbiamo lasciato di tenersi le query solo se DEBUG = SI //if($GLOBALS["CONF"]["DEBUG"] == "SI" or indi_location() == "loc") { if($GLOBALS["CONF"]["DEBUG"] == "SI") { $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("$query","db:query(QT:$TIME)","DATAB"); $DB["query"][] = array($query,$TIME); } if ($GLOBALS["indi_stats_db"]) { $dbq_diff = microtime(true) - $dbq_start; $GLOBALS["indi_stats_db_tempo_db"] += $dbq_diff; } if ($no_link) mysql_close($link); return $result; } if ($no_link) mysql_close($link); return FALSE; } //forza utf8 function db_q_utf8($query,$database=NULL) { global $DB; //salvo $charset_old = $GLOBALS['CONF']['mcharset']; $GLOBALS['CONF']['mcharset'] = "UTF-8"; $q = db_q($query,$database); //ripristino $GLOBALS['CONF']['mcharset'] = $charset_old; return $q; } //FUNZIONI DB VERSIONE 2.2 function db_q_indi($query) { return db_q($query,"indi"); } //ritorna una array delle righe function db_crea($database) { global $DB; if (!$link = @mysql_connect($DB[host], $DB[username], $DB[password])) { $result = FALSE; d_err("Errore nella connessione a $DB[host]!","pconnect","DATAB"); } $out = mysql_create_db("$database"); return $out; } //ritorna una sola riga || apc function db_qr($query,$database=NULL) { global $DB; if($GLOBALS["DATI"]['indi_apc_all_query'] == "SI" and $database == NULL) { return db_qr_cache($query); } $q = db_q($query,$database); if($q) { $riga = mysql_fetch_assoc($q); //mysql_free_result($q); return $riga; } else { return false; } } function db_qr_cache($query) { global $DB; if($GLOBALS["CONF"]["DEBUG"] == "SI") $db_time = indi_time(); $key = "db_qr_cache:".md5($GLOBALS["DB"].$query); //cache apc if($GLOBALS["DATI"]["indi_apc"] == "SI" && !$GLOBALS["DATI"]["disable_db_cache"] && !$_GET['no_db_cache'] && !$_GET['no_cache']) { //apc $mk = apc_fetch($key); if ($mk) { if($_GET['indi_apc']) print "k"; if($GLOBALS["CONF"]["DEBUG"] == "SI") { //debug $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("","apc:OUT:db_qr_cache(QT:$TIME)","DATAB"); } return unserialize($mk); } else { $q = db_q($query); if($q) { $result = mysql_fetch_assoc($q); apc_store($key,serialize($result),$GLOBALS["DATI"]['indi_apc_time']); if($GLOBALS["CONF"]["DEBUG"] == "SI") { //debug $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("","apc:IN:db_qr_cache(QT:$TIME)","DATAB"); } //mysql_free_result($q); return $result; } else { return false; } } } else { if($GLOBALS["DATI"]["indi_apc"] == "SI") apc_delete($key); if($GLOBALS["CONF"]["DEBUG"] == "SI") { //debug $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("","apc:KO:db_qr_cache(QT:$TIME)","DATAB"); } return db_qr($query); } } //ritorna una array delle righe function db_qa($query,$database_solo_per_indi_maggiore_di_6=NULL) {//se si usa il secondo parametro con indi < 6 succede casino, meglio specificare il database all'interno della query global $DB; if($GLOBALS["DATI"]['indi_apc_all_query'] == "SI") { return db_qa_cache($query,$database_solo_per_indi_maggiore_di_6); } if($GLOBALS[CONF]["indi_subversion"] < 6){ $q = db_q($query); } else { $q = db_q($query,$database_solo_per_indi_maggiore_di_6); } if($q) { if($GLOBALS[CONF]["indi_subversion"] > 5) $array = array();//c3k: cosė ritorna sempre un tipo di dato array() e non da' errori sui foreach /* dentro molti pagine_dinamiche_tpl_1.inc.php viene fatto $sottopagine == "" e echo var_dump(null == "");// => true echo var_dump(array() == "");// => false */ while ($riga = mysql_fetch_assoc($q)) { $a = $riga[$database_solo_per_indi_maggiore_di_6]; //print "+$a+"; if($database_solo_per_indi_maggiore_di_6 != NULL and $GLOBALS[CONF]["indi_subversion"] < 6) $array[$a] = $riga; else $array[] = $riga; } //mysql_free_result($q); return $array; } else { return false; } } //ritorna una array delle righe function db_qa_cache($query,$chiave=NULL) { if($GLOBALS["CONF"]["DEBUG"] == "SI") $db_time = indi_time(); global $DB; if($GLOBALS["DATI"]["indi_apc"] == "SI" && !$GLOBALS["DATI"]["disable_db_cache"] && !$_GET['no_db_cache'] && !$_GET['no_cache']) { //apc $key = "db_qa_cache:".md5($GLOBALS["DB"].$query.$chiave); $mk = apc_fetch($key); if ($mk) { if($_GET['indi_apc']) print "k"; if($GLOBALS["CONF"]["DEBUG"] == "SI") { //debug $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("","apc:OUT:$key(QT:$TIME)","DATAB"); } return unserialize($mk); } else { $q = db_q($query); if($q) { while ($riga = mysql_fetch_assoc($q)) { $a = $riga[$chiave]; if($chiave != NULL) $array["$a"] = $riga; else $array[] = $riga; } apc_store($key, serialize($array), $GLOBALS["DATI"]['indi_apc_time']); if($GLOBALS["CONF"]["DEBUG"] == "SI") { //debug $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("","apc:IN:$key(QT:$TIME)","DATAB"); } //mysql_free_result($q); return $array; } else { return false; } } } if($GLOBALS["CONF"]["DEBUG"] == "SI") { //debug $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("","apc:KO:db_qa_cache(QT:$TIME)","DATAB"); } return db_qa($query,$chiave); } //ritorna una array delle righe ??? function db_qa_indi($query,$chiave=NULL) { if($GLOBALS["CONF"]["DEBUG"] == "SI") $db_time1 = indi_time(); global $DB; $q = db_q($query,"indi"); while ($riga = mysql_fetch_assoc($q)) { $a = $riga[$chiave]; //print "+$a+"; if($chiave) $array["$a"] = $riga; else $array[] = $riga; } //mysql_free_result($q); return $array; } function db_qrs($query, $campo=0, $database=NULL, $databaseCharset=null) { global $DB; if($GLOBALS["DATI"]['indi_apc_all_query'] == "SI") { return db_qrs_cache($query,$campo); } $q = db_q($query,$database,$databaseCharset); if($q) { $riga = mysql_fetch_array($q); //mysql_free_result($q); return $riga[$campo]; } else { return false; } } function db_qrs_cache($query,$campo=0) { global $DB; if($GLOBALS["CONF"]["DEBUG"] == "SI") $db_time = indi_time(); if($GLOBALS["DATI"]["indi_apc"] == "SI" && !$GLOBALS["DATI"]["disable_db_cache"] && !$_GET['no_db_cache'] && !$_GET['no_cache']) { //apc $key = "db_qrs_cache:".md5($GLOBALS["DB"].$query.$campo); $mk = apc_fetch($key); if ($mk) { if($_GET['indi_apc']) print "k"; if($GLOBALS["CONF"]["DEBUG"] == "SI") { //debug $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("","apc:OUT:db_qrs_cache($key, ".$GLOBALS["DATI"]['indi_apc_time'].")(QT:$TIME)","DATAB"); } return $mk; } else { $q = db_q($query); if($q) { $riga = mysql_fetch_array($q); apc_store($key, $riga[$campo], $GLOBALS["DATI"]['indi_apc_time']); if($GLOBALS["CONF"]["DEBUG"] == "SI") { //debug $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("","apc:IN:db_qrs_cache($key, ".$GLOBALS["DATI"]['indi_apc_time'].")(QT:$TIME)","DATAB"); } //mysql_free_result($q); return $riga[$campo]; } else { return false; } } } if($GLOBALS["CONF"]["DEBUG"] == "SI") { //debug $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("","apc:KO:db_qrs_cache(".$GLOBALS["DATI"]['indi_apc_time'].")(QT:$TIME)","DATAB"); } return db_qrs($query,$campo); } function db_qf($query,$funzione="db_qf_array") { //funzione di base function db_qf_array($V){ return $V; } global $DB; $q = db_q($query); while ($V = mysql_fetch_assoc($q)) { $out[] = $funzione($V); } //mysql_free_result($q); return $out; } //queri con risultati separati da un separatore function db_qsep($query,$campo,$sep="|") { global $DB; $q = db_q($query); while ($riga = mysql_fetch_assoc($q)) { $a = $riga[$campo]; //print "+$a+"; $out .= $a.$sep; } //mysql_free_result($q); return $out; } //queri con risultati in un array chiave valore function db_qkv_cache($query,$kk=NULL,$vv=NULL,$ARR=NULL) { global $DB; if($GLOBALS["CONF"]["DEBUG"] == "SI") $db_time = indi_time(); if($GLOBALS["DATI"]["indi_apc"] == "SI" && !$GLOBALS["DATI"]["disable_db_cache"] && !$_GET['no_db_cache'] && !$_GET['no_cache']) { //apc $key = "db_qkv_cache:".md5($GLOBALS["DB"].$query.$kk.$vv); $mk = apc_fetch($key); if ($mk) { if($GLOBALS["CONF"]["DEBUG"] == "SI") { //debug $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("","apc:OUT:db_qkv_cache($key, ".$GLOBALS["DATI"]['indi_apc_time'].")(QT:$TIME)","DATAB"); } return $mk; } else { $q = db_q($query); if($q) { if(is_array($ARR)) $out = $ARR; while ($riga = mysql_fetch_assoc($q)) { if($vv === NULL) { if($kk == NULL) $out[] = $riga[0]; else $out[] = $riga[$kk]; } else { $k = $riga[$kk]; $v = $riga[$vv]; $out[$k] = $v; } } apc_store($key,$out, $GLOBALS["DATI"]['indi_apc_time']); if($GLOBALS["CONF"]["DEBUG"] == "SI") { //debug $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("","apc:IN:db_qrs_cache($key, ".$GLOBALS["DATI"]['indi_apc_time'].")(QT:$TIME)","DATAB"); } //mysql_free_result($q); return $out; } else { return false; } } } if($GLOBALS["CONF"]["DEBUG"] == "SI") { //debug $db_time2 = indi_time(); $TIME = $db_time2 - $db_time; d_info("","apc:KO:db_qkv_cache(".$GLOBALS["DATI"]['indi_apc_time'].")(QT:$TIME)","DATAB"); } return db_qkv($query,$kk,$vv,$ARR); } //queri con risultati in un array chiave valore function db_qkv($query,$kk=NULL,$vv=NULL,$ARR=NULL) { global $DB; if($GLOBALS["DATI"]['indi_apc_all_query'] == "SI") { return db_qkv_cache($query,$kk,$vv,$ARR); } $q = db_q($query); if($q) { if(is_array($ARR)) $out = $ARR; while ($riga = mysql_fetch_array($q)) { if($vv === NULL) { if($kk == NULL) $out[] = $riga[0]; else $out[] = $riga[$kk]; } else { $k = $riga[$kk]; $v = $riga[$vv]; $out[$k] = $v; } } //mysql_free_result($q); return $out; } else { return false; } } function db_restore_dump($filename,$database=NULL){ $templine = ''; $lines = file($filename); foreach ($lines as $line){ if (substr($line, 0, 2) == '--' || $line == '') continue; $templine .= $line; if (substr(trim($line), -1, 1) == ';'){ db_q($templine,$database); $templine = ''; } } } function db_create_db($nome, $charset=null){ db_connect_only();//credenziali db_q("CREATE DATABASE IF NOT EXISTS ".$nome.($charset?" CHARACTER SET $charset":"")); return db_connect();//seleziona db } function db_connect_only(){ global $DB; $DB['link'] = mysql_connect($DB['host'], $DB['username'], $DB['password']); return $DB['link']; } function db_connect($tipo="link") { global $DB; if (!$link = mysql_connect($DB[host], $DB[username], $DB[password])) { return false; $result = 0; print ("
Errore nella connessione a $db_host!"); } else { if (!@mysql_select_db($DB[database], $link)) { return false; $result = 0; print ("
Errore nella selezione del
"); } } if($tipo == "link") $DB['link'] = $link; return true; } function db_connect2() { global $db, $var_Global; $db = mysql_connect($var_Global["db_host"], $var_Global["db_username"], $var_Global["db_password"]); if (!$db) die("Impossibile collegarsi al database\n"); if (!mysql_select_db($var_Global["db_nome"], $db)) die("Impossibile selezionare il database\n"); } function db_query($sql,$msg="") { return db_q($sql); // global $DB; // // // $res = mysql_query($sql, $DB[link]); // // if(!$res) { // // print "
ERRORE: ".mysql_error()." query SQL: [ $sql ]"; // // if($msg){ // echo " - $msg
"; // die(); // } // // } // // return $res; } function db_lastid($tabella,$campo) { global $DB; $res=db_query("select $campo from $tabella order by $campo desc limit 1","Impossibile selezionare l'ultimo id"); list($lastid) = mysql_fetch_row($res); return $lastid + 1; } function db_appendi_condizioni_sql($sql, $condizioni) { //se esiste, ciclo l'array delle condizioni e l'appendo alla query if(is_array($condizioni)) { $tot = sizeof($condizioni); for ($i=0; $i<$tot; $i++) { if ($i==0) { $sql .= "where "; } $sql .= $condizioni[$i]; if ($i<($tot-1)) { $sql .= " and "; } } } return $sql; } function db_cerca_parola($parola) { if(trim($parola) != "") return "'%$parola%'"; } //crea la WHERE per cercare una parola composta //da finire gli altri tipi //ricordarsi se gpc di fare l' escape e togliere gli slash function db_cerca_parola_like($parola,$campo,$verso="ALL") { $parola = trim(mysql_real_escape_string($parola)); if($parola == "") return NULL; $query_raw_arr = explode(" ", $parola); $q .= "("; for ($i=0; $i 0) $q .= " OR "; if($verso == "ALL") $q .= "$campo LIKE '%".$query_raw_arr[$i]."%'"; elseif($verso == "SX") $q .= "$campo LIKE '%".$query_raw_arr[$i]."%'"; elseif($verso == "DX") $q .= "$campo LIKE '%".$query_raw_arr[$i]."%'"; } $q .= ")"; return $q; } // $DB["mssql_server"] = "ita.afsglobal.org"; // $DB["mssql_username"] = "sa"; // $DB["mssql_password"] = "ITAmaestro06"; // $DB["mssql_database"] = db_intercultura; // $DB["mssql_callback"] = "intercultura_mssql_callback"; // PARAM => database, utf8, redirect function ms_db_q($param){ global $DB; $default_param["database"] = $DB["mssql_database"]; $default_param["mssql_callback"] = $DB["mssql_callback"]; if($DB["mssql_charset"]) $default_param["mssql_charset"] = $DB["mssql_charset"]; else $default_param["mssql_charset"] = 'UTF-8'; $default_param["redirect"] = false; if(!is_array($param)){ $param = array("query" => $param); } $array = array_merge($default_param, $param); if($array["mssql_charset"]) ini_set('mssql.charset', $array["mssql_charset"]); if(!$array["database"] || !$DB["mssql_server"] || !$DB["mssql_username"] || !$DB["mssql_password"]){ if($GLOBALS['CONF']['dove_sono'] == 'loc'){ trigger_error("Paramentri database MSSQL non impostati"); } return false; } $link = @mssql_connect($DB["mssql_server"], $DB["mssql_username"], $DB["mssql_password"]); if(!@mssql_select_db($array["database"], $link)){ if($link) mssql_close($link); return false; } @mssql_query(" SET ANSI_NULLS ON SET ANSI_PADDING ON SET ANSI_WARNINGS ON SET ARITHABORT ON SET CONCAT_NULL_YIELDS_NULL ON SET QUOTED_IDENTIFIER ON SET NUMERIC_ROUNDABORT OFF "); $errore = !$link; if(!$errore){ $result = mssql_query($array["query"], $link); $errore = !$result; }elseif($array["redirect"]){ $array["mssql_callback"](array("errore_connessione" => 1)); } if($array["mssql_callback"] != ""){ $log_arr = array("query" => $array["query"]); if($errore){ $log_arr["errore"] = mssql_get_last_message(); } $array["mssql_callback"]($log_arr); } //Returns a MS SQL result resource on success, TRUE if no rows were returned, or FALSE on error. if(!$result){ $return = false; }elseif($result === true){ $return = true; }else{ while ($row = mssql_fetch_assoc($result)) { $return[] = $row; } } mssql_close($link); return $return; } function ms_db_qa($param){ $cacheKey = ''; if(!$GLOBALS["DATI"]["disable_db_cache"] && !$_GET['no_db_cache'] && !$_GET['no_cache'] && is_array($param) && $param["cache"]){ $cacheKey = md5(serialize($param)); $cached = indi_apc_fetch($cacheKey); if($cached !== false) return $cached; } $result = ms_db_q($param); if(!$result){ $return = false; }else{ if(is_array($param) && $param["cache"]){ indi_apc_store($cacheKey, $result, $param["cache"]); } $return = $result; } return $return; } function ms_db_qr($param){ $result = ms_db_qa($param); if(!$result){ return false; }else{ return array_shift($result); } } function ms_db_qrs($param){ $result = ms_db_qa($param); if(!$result){ return false; }else{ $array = array_values(array_shift($result)); return $array[0]; } } ?>