class mysql_db_connect {
//private variables
var $dbLink; //Link to database
var $dbName; //Name of databse
var $hostName; //Name of server
var $dbUser; //Database user name
var $dbPwd; //Database password
var $dbQryResult; //Result of last query
var $dbResultLine; //One Result line of last query
var $keepLog; //Should a log of all queries be kept
var $logPath; //Path where log should be saved
var $fullErr; //Display full or partial error
var $rollBackSup; //Are you using tables with transactional support (ie. InnoDB)
// Constructor
// Creates object and makes connection to database.
function mysql_db_connect($banco_master="") {
// Read config info from cfg file and saves them in class
require(CLASSDIR . "/_mysqlconn.class.php");
if(!$banco_master)
$db = $_SESSION['db_name'];
$this->dbName = $db;
$this->hostName = $host;
$this->dbUser = $user;
$this->keepLog = $keeplog;
$this->logPath = $logpath;
$this->fullErr = $fullErr;
$this->rollBackSup = $rollBackSup;
if (strlen($pwd) > 0) { $this->dbPwd = "Yes"; } else { $this->dbPwd = "No"; }
global $charset;
$this->charset = ($charset) ? $charset : $defaultCharset;
// Creates connection to server and selects appropriate database
$dbLink = mysql_connect($this->hostName, $this->dbUser, $pwd) or $this->goError($qry, mysql_error());
mysql_select_db($this->dbName) or $this->goError($qry, mysql_error());
mysql_set_charset($this->charset) or $this->goError($qry, mysql_error());
}
// Displays the end of the error message
function endError($err, $from) {
// Displays error
print $err."
";
// Displays conection details as well as the page using this class if set
if ($this->fullErr == 1) {
print "System variables: ";
print "Host: ".$this->hostName." ";
print "Database: ".$this->dbName." ";
print "User: ".$this->dbUser." ";
print "Password Used: ".$this->dbPwd." ";
print "Refering Page: ".$from." ";
}
print "";
print "
Please contact your system administrator for assistance!
";
// Make sure script dies
die(date(r, time()));
}
// Displays error if a rollback could not be done.
function rollError($err, $from) {
print "ERROR: ";
print "";
if ($this->fullErr == 1) {
print "Query:$qry ";
}
print "Could not roll back transaction!!
\n";
$this->endError($err, $from);
}
// If something has gone wrong, displays an error
function goError($qry, $err, $from) {
// Rolls back transaction if required
if ($this->rollBackSup == 1) {
print "Transaction rolled back!
\n";
mysql_query("rollback;") or $this->rollError(mysql_error());
}
print "ERROR: ";
print "";
if ($this->fullErr == 1) {
print "Query:$qry ";
}
$this->endError($err, $from);
}
// Executes a query ($qry). $from is the page you are calling from - optional
function db_qry($qry, $from="") {
$this->dbQryResult = mysql_query($qry) or $this->goError($qry, mysql_error(), $from);
// If required, write query to a log file
if ($this->keepLog == 1) {
$fp = fopen($this->logPath."mysql_db_class.log", "a+");
fputs($fp, date("r", time())." - ".$from."\n");
fputs($fp, $qry."\n");
fclose($fp);
}
$qtyLines = mysql_affected_rows();
return $qtyLines;
}
// Retorna ultimo id inserido
function get_last_id() {
$this->dbQryResult = mysql_query("SELECT LAST_INSERT_ID()");
list($last_id) = mysql_fetch_array($this->dbQryResult);
return $last_id;
}
// Returns the resultset, one row at a time in an array
function get_data() {
$this->dbResultLine = mysql_fetch_array($this->dbQryResult);
return $this->dbResultLine;
}
// Number of rows in resultset
function row_count() {
$qtyLines = mysql_num_rows($this->dbQryResult);
return $qtyLines;
}
// Array with field names of resultset
function get_fields() {
$fldList = Array();
for ($fldCntr = 0; $fldCntr < mysql_num_fields($this->dbQryResult); $fldCntr++) {
$fldList[$fldCntr] = mysql_field_name($this->dbQryResult, $fldCntr);
}
return $fldList;
}
// Retorna Linhas modificadas
function affected_rows() {
return mysql_affected_rows();
}
}
?>
PagAqui Serviços Financeiros