_errorCode = 0; $this->_errorMsg = ''; $this->_debug = false; $this->_startTime = 0; $this->_execTime = 0; $this->database=$dbName; $this->_conn = @mysqli_connect($dbServer, $dbUser, $dbPass); //mysqli_select_db($dbName, $this->_conn) or die ("Não foi possível abrir a Base-de-Dados $BaseDados.
".mysqli_error()); if(!$this->_conn){ $this->printError('Não foi possível ligar ao servidor MySQL da INOVESAUDE...'.$dbName, true); } elseif(!mysqli_select_db($this->_conn,$dbName)){ $this->printError('Erro ao seleccionar a base de dados '.$dbName); } return $this->_conn; } function sanitize($str) { return mysqli_real_escape_string($this->_conn, $str); } function moveFirst($result) { return mysqli_data_seek($result, 0); } function printError($msg='MySQL ERROR', $showErrors=false){ echo '
'; echo $msg; if(mysqli_errno($this->_conn) && ($showErrors || $this->_debug)){ echo '
'.mysqli_error($this->_conn); } echo '
'; } function getCurrentPage(){ return $this->_currentPage; } function getTotalPages(){ return $this->_totalPages; } function getTotalRecords(){ return $this->_totalRecords; } function execute($query){ $this->_startTime = (float)microtime(); if ($this->getItemsPerPage() >0 ) { if ( isset($_GET['page']) && intval($_GET['page'])>=0) { // get total rows without limits if ($this->_numRowsQuery != "") $numRowsQuery = $this->_numRowsQuery; else $numRowsQuery = $query; $r = mysqli_query($this->_conn,$numRowsQuery); if($r) { $this->_totalPages = ceil(mysqli_num_rows($r) / $this->getItemsPerPage() ); $this->_totalRecords = mysqli_num_rows($r); } mysqli_free_result($r); // apply limits if (intval($_GET['page']) == 0 ) { $currentPage= quote_smart(intval($_GET['page'])) * $this->getItemsPerPage(); $this->_currentPage = quote_smart(intval($_GET['page'])+1); } else { $currentPage= quote_smart(intval($_GET['page'])-1) * $this->getItemsPerPage(); $this->_currentPage = quote_smart(intval($_GET['page'])); } $query = $query . " limit $currentPage, " . $this->getItemsPerPage(); } else { $query = $query . " limit 0, " . $this->getItemsPerPage(); } } $r = mysqli_query( $this->_conn, $query); if ($r) $this->_last_inserted_id = mysqli_insert_id($this->_conn); $this->_execTime = (float)microtime() - $this->_startTime; if($this->_debug){ echo '
'.$query.'('.$this->_execTime.' ms)
'; } if(($_SERVER['HTTP_HOST'] == 'localhost') && mysqli_errno($this->_conn)){ $this->printError($query, true); } $this->setItemsPerPage(-1,""); return $r; } function free_result($result) { return mysqli_free_result($result); } function getAll($query) { $this->_startTime = (float)microtime(); $result = mysqli_query($this->_conn,$query); $this->_execTime = (float)microtime() - $this->_startTime; $tmpArr = array(); while ( $fecthArr = mysqli_fetch_assoc($result) ) array_push($tmpArr, $fecthArr); return $tmpArr; } function getRow($query){ $this->_startTime = (float)microtime(); $r = mysqli_query($this->_conn,$query); $this->_execTime = (float)microtime() - $this->_startTime; if($this->_debug){ echo '
'.$query.'('.$this->_execTime.' ms)
'; } if(($_SERVER['HTTP_HOST'] == 'localhost') && mysqli_errno($this->_conn)){ $this->printError($query, true); } if($r){ return mysqli_fetch_assoc($r); } return null; } function getConnection(){ return $this->_conn; } function getItemsPerPage(){ return $this->_itemsPerPage; } function setItemsPerPage($value=-1,$numRowsQuery = ""){ $this->_itemsPerPage = $value; $this->_numRowsQuery = $numRowsQuery; } function executeTime(){ return $this->_execTime; } function fetchRow($r){ return mysqli_fetch_assoc($r); } function selectDB($dbName, $dbServer = "", $username = "", $password = ""){ global $db; if($dbName && $username == "" && $password == ""){ /* if($username == "" && $password == ""){ $this->__construct('licencas', 'licencas_usr', 'kR7fc*47','localhost'); $clientDB = $db->getRow(sprintf("select basedados, utilizadorbasedados, passbasedados from licencas_software where dominio = '%s'", $db->sanitize($_SERVER['HTTP_HOST']))); $username = $clientDB['utilizadorbasedados']; $password = $clientDB['passbasedados']; } */ $this->__construct($dbName, $username, $password, $dbServer); $this->database=$dbName; return true; }elseif($dbName){ $dbServer = ($dbServer != "") ? $dbServer : "localhost"; $this->__construct($dbName, $username, $password, $dbServer); $this->database=$dbName; return true; } return false; } function debug($val = true){ $this->_debug = $val; } function numRows($r){ if($r) return mysqli_num_rows($r); else return null; } function getLastInsertedId(){ return $this->_last_inserted_id; } function affectedRows(){ return mysqli_affected_rows($this->_conn); } function getField($queryResult,$field){ mysqli_data_seek($queryResult,0); $dados = $this->fetchRow($queryResult); return $dados[$field]; } function printRecordset($r, $fields = null){ echo '
'; $i = 0; while($row = $this->fetchRow($r)){ foreach($row as $k=>$v){ if($fields){ if(array_key_exists($k, $fields)){ echo $v.' , '; } } else echo $v.' , '; } echo '
'; $i++; } echo '
'; } // INSERT / UPDATE function autoExecute($table_name = '', $fields = array(), $query_type = '', $where_conditions = null){ $i = 0; $query = ''; if(!$table_name) return null; if(!is_array($fields) || !count($fields)) return null; switch(strtoupper($query_type)){ case 'INSERT': $queryFields = ''; $queryValues = ''; foreach($fields as $key => $value){ if($i) { $queryFields.=', '; $queryValues.=', '; } if (is_array($value)) { if ($value[1]!="string") { if(!is_numeric($value[0])) $value = '\''.addslashes($value[0]).'\''; else $key.'='.$value[0]; } else { $value = '\''.addslashes($value[0]).'\''; } } else if(!is_numeric($value)) $value = '\''.addslashes($value).'\''; $queryFields.= $key; $queryValues.= $value; $i++; } $query = 'INSERT INTO '.$table_name.'('.$queryFields.') VALUES('.$queryValues.')'; $r = $this->execute($query); if($r) $this->_last_inserted_id = mysqli_insert_id($this->_conn); else $this->_last_inserted_id = 0; return $r; case 'UPDATE': $queryFields = ''; foreach($fields as $key => $value){ if($i) $queryFields.=', '; if (is_array($value)) { if ($value[1]!="string") { if(!is_numeric($value[0])) $value = '\''.addslashes($value[0]).'\''; else $key.'='.$value[0]; } else { $value = '\''.addslashes($value[0]).'\''; } } else if(!is_numeric($value)) $value = '\''.addslashes($value).'\''; $queryFields.= $key.'='.$value; $i++; } $query = 'UPDATE '.$table_name.' SET '.$queryFields; if($where_conditions) $query.=' WHERE '.$where_conditions; return $this->execute($query); } return null; } } if (! function_exists("set_magic_quotes_runtime")) { function set_magic_quotes_runtime(){ return true; } } function quote_smart($value,$quotes = true) { global $db; // Stripslashes if (get_magic_quotes_gpc()) { $value = stripslashes($value); } // Quote if not a number or a numeric string if (!is_numeric($value)) { if ($quotes) $value = "'" . mysqli_real_escape_string($db->_conn,$value) . "'"; else { $value = mysqli_real_escape_string($db->_conn,$value) ; } } return $value; } $db_licencas = new DB("licencas_vendse", "licencas_usr", "pX~z9r88"); $get_acesso_bd = $db_licencas->getRow("SELECT * FROM licencas WHERE dominio='".$_SERVER['HTTP_HOST']."'"); $db = new DB($get_acesso_bd['base_dados'], $get_acesso_bd['user_base_dados'], $get_acesso_bd['password_base_dados']);?> VENDSE