Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //DBMySQL Class
- /*
- * Database Management for PHP
- *
- * Copyright (c) 2009/2010
- * by Gledston Reis(gledston.reis@catolica.edu.br)
- *
- * Licença: GPL
- * mysql_class.php
- *
- */
- class DB_MySQL {
- /* público: paramentros de conexões */
- public $DBHost = "";
- public $DBPort = "";
- public $DBDatabase = "";
- public $DBUser = "";
- public $DBPassword = "";
- public $Persistent = false; ## conexão persistente. Default false
- /* público: configuração do padrões */
- public $Auto_Free = 1; ## 1 para automática chamada da função mysql_free_result()
- public $Debug = 0; ## 1 para informações de debug. Default 0.
- public $Seq_Table = "db_sequence";
- /* público: resultado em array */
- public $Record = array();
- public $Row;
- /* público: erro e número do erro */
- public $Errno = 0;
- public $Error = "";
- /* público: versão dessa classe */
- public $type = "mysql";
- public $revision = "1.0";
- /* privado: handle e link de conexão */
- private $Link_ID = 0;
- private $Query_ID = 0;
- private $Connected = false;
- private $Encoding = "";
- /* público: construtor */
- function __construct($query = "") {
- $this->query($query);
- }
- /* público: link id */
- function link_id() {
- return $this->Link_ID;
- }
- function query_id() {
- return $this->Query_ID;
- }
- function try_connect($DBHost = "", $DBPort = "", $DBUser = "", $DBPassword = "") {
- $this->Query_ID = 0;
- /* Padrões */
- if ("" == $DBHost) $DBHost = $this->DBHost;
- if ("" == $DBPort) $DBPort = $this->DBPort;
- if ("" == $DBUser) $DBUser = $this->DBUser;
- if ("" == $DBPassword) $DBPassword = $this->DBPassword;
- if($DBPort != "") $DBHost .= ":" . $DBPort;
- if($this->Persistent)
- $this->Link_ID = @mysql_pconnect($DBHost, $DBUser, $DBPassword);
- else
- $this->Link_ID = @mysql_connect($DBHost, $DBUser, $DBPassword);
- $this->Connected = $this->Link_ID ? true : false;
- return $this->Connected;
- }
- /* público: gerenciamento de conexões*/
- function connect($DBDatabase = "", $DBHost = "", $DBPort = "", $DBUser = "", $DBPassword = "") {
- /* padrões */
- if ("" == $DBDatabase) $DBDatabase = $this->DBDatabase;
- if ("" == $DBPort) $DBPort = $this->DBPort;
- if ("" == $DBHost) $DBHost = $this->DBHost;
- if ("" == $DBUser) $DBUser = $this->DBUser;
- if ("" == $DBPassword) $DBPassword = $this->DBPassword;
- if($DBPort != "") $DBHost .= ":" . $DBPort;
- /* estabelece a conexão com a base de dados */
- if (!$this->Connected) {
- $this->Query_ID = 0;
- if($this->Persistent)
- $this->Link_ID=@mysql_pconnect($DBHost, $DBUser, $DBPassword);
- else
- $this->Link_ID=@mysql_connect($DBHost, $DBUser, $DBPassword);
- if (!$this->Link_ID) {
- $this->halt("Impossível conectar a base de dados " . mysql_error());
- return 0;
- }
- if (!@mysql_select_db($DBDatabase,$this->Link_ID)) {
- $this->halt("Impossivel usar a base de dados " . mysql_error());
- return 0;
- }
- $server_info = @mysql_get_server_info($this->Link_ID);
- preg_match("/\d+\.\d+(\.\d+)?/", $server_info, $matches);
- $version_str = $matches[0];
- $version = explode(".", $version_str);
- if ($version[0] >= 4) {
- if (($version[0] > 4 || $version[1] >= 1) && is_array($this->Encoding) && $this->Encoding[1])
- @mysql_query("SET NAMES '" . $this->Encoding[1] . "'", $this->Link_ID);
- elseif (is_array($this->Encoding) && $this->Encoding[0])
- @mysql_query("SET NAMES '" . $this->Encoding[0] . "'", $this->Link_ID);
- }
- $this->Connected = true;
- }
- return $this->Link_ID;
- }
- /* público: limpa o resultado da query */
- function free_result() {
- if (is_resource($this->Query_ID)) {
- @mysql_free_result($this->Query_ID);
- }
- $this->Query_ID = 0;
- }
- /* público: query */
- function query($Query_String) {
- if ($Query_String == "")
- return 0;
- if (!$this->connect()) {
- return 0; /* nós ja estamos conectados. */
- };
- # Nova query, descarte a última.
- if ($this->Query_ID) {
- $this->free_result();
- }
- if ($this->Debug)
- printf("Debug: query = %s<br>\n", $Query_String);
- $this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
- $this->Row = 0;
- $this->Errno = mysql_errno();
- $this->Error = mysql_error();
- if (!$this->Query_ID) {
- die("Database Error: " . mysql_error());
- }
- # retornaremos nada se falhar.
- return $this->Query_ID;
- }
- /* público: navega no resultado */
- function next_record() {
- if (!$this->Query_ID)
- return 0;
- $this->Record = @mysql_fetch_array($this->Query_ID);
- $this->Row += 1;
- $this->Errno = mysql_errno();
- $this->Error = mysql_error();
- $stat = is_array($this->Record);
- if (!$stat && $this->Auto_Free) {
- $this->free_result();
- }
- return $stat;
- }
- /* público: posição no registro */
- function seek($pos = 0) {
- $status = @mysql_data_seek($this->Query_ID, $pos);
- if ($status) {
- $this->Row = $pos;
- } else {
- die("Database error: seek($pos) falhou - o resultado foi ".$this->num_rows()." rows");
- @mysql_data_seek($this->Query_ID, $this->num_rows());
- $this->Row = $this->num_rows();
- }
- return true;
- }
- /* público: trava a tabela */
- function lock($table, $mode="write") {
- $this->connect();
- $query="lock tables ";
- if (is_array($table)) {
- while (list($key,$value)=each($table)) {
- if ($key=="read" && $key!=0) {
- $query.="$value read, ";
- } else {
- $query.="$value $mode, ";
- }
- }
- $query=substr($query,0,-2);
- } else {
- $query.="$table $mode";
- }
- $res = @mysql_query($query, $this->Link_ID);
- if (!$res) {
- die("Database error: Não foi possivel travar a tabela - " . mysql_error());
- return 0;
- }
- return $res;
- }
- /*público: destrava a tabela */
- function unlock() {
- $this->connect();
- $res = @mysql_query("unlock tables");
- if (!$res) {
- die("Database error: não foi possível destravar a tabela - " . mysql_error());
- return 0;
- }
- return $res;
- }
- /* público: funções de efeito da query */
- function affected_rows() {
- return @mysql_affected_rows($this->Link_ID);
- }
- function num_rows() {
- return @mysql_num_rows($this->Query_ID);
- }
- function num_fields() {
- return @mysql_num_fields($this->Query_ID);
- }
- function f($Name) {
- return $this->Record && array_key_exists($Name, $this->Record) ? $this->Record[$Name] : "";
- }
- function p($Name) {
- print $this->Record[$Name];
- }
- function close()
- {
- if ($this->Query_ID) {
- $this->free_result();
- }
- }
- /* privado: erros */
- function halt($msg) {
- printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
- printf("<b>MySQL Error</b><br>\n");
- die("Session halted.");
- }
- }
- //End DB MySQL Class
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement