📄 mysql.php
字号:
<?php
// +-------------------------------------------------------------+
// | DeskPRO v [2.0.1 Production]
// | Copyright (C) 2001 - 2004 Headstart Solutions Limited
// | Supplied by WTN-WDYL
// | Nullified by WTN-WDYL
// | Distribution via WebForum, ForumRU and associated file dumps
// +-------------------------------------------------------------+
// | DESKPRO IS NOT FREE SOFTWARE
// +-------------------------------------------------------------+
// | License ID : Full Enterprise License =) ...
// | License Owner : WTN-WDYL Team
// +-------------------------------------------------------------+
// | $RCSfile: mysql.php,v $
// | $Date: 2004/02/10 01:34:25 $
// | $Revision: 1.43 $
// +-------------------------------------------------------------+
// | File Details:
// | - MySQL database access class
// +-------------------------------------------------------------+
class DB_Sql {
var $host = "localhost";
var $database = "";
var $user = "root";
var $password = "";
var $Halt_On_Error = "yes";
var $Warning_On_Error = "no";
var $errno = 0;
var $error = "";
var $link_id = 0;
var $last_id_num = 0;
var $query_id = 0;
var $record = array();
var $appname = "DeskPRO";
###################### function connect() #######################
/* Connects to the mySQL database server */
function connect($database = "", $host = "", $user = "", $password = "") {
if ("" == $database)
$database = $this->Database;
if ("" == $host)
$host = $this->Host;
if ("" == $user)
$user = $this->User;
if ("" == $password)
$password = $this->Password;
if ( 0 == $this->Link_ID ) {
$this->Link_ID=@mysql_connect($host, $user, $password);
if (!$this->Link_ID) {
$this->halt("connect($host, $user, \$password) failed. (Cannot connect to server)");
return 0;
}
$this->select_db($this->Database);
}
return $this->Link_ID;
}
###################### function free() #######################
/* Frees memory */
function free() {
@mysql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
################### function select_db() #####################
/* Select a database */
function select_db($dbname) {
if (!@mysql_select_db($dbname, $this->Link_ID)) {
$this->halt("cannot use database $dbname");
return 0;
} else {
$this->Database = $dbname;
return $this->Link_ID;
}
}
###################### function query() #######################
/* Run a query */
function query($Query_String) {
global $query_count, $showqueries, $query_info, $query_log;
if ($Query_String == "")
return 0;
if (!$this->connect()) {
return 0;
};
if ($this->Query_ID) {
$this->free();
}
if (defined('LOGQUERIES') AND !defined('DONOT_LOGQUERIES')) {
list ($usec, $sec) = explode(" ",microtime());
$start = ((float)$usec + (float)$sec);
}
$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) {
$this->halt("Invalid SQL: ".$Query_String);
}
if (defined('LOGQUERIES') AND !defined('DONOT_LOGQUERIES')) {
$this->last_id_num = mysql_insert_id($this->Link_ID);
list ($usec, $sec) = explode(" ",microtime());
$stop = ((float)$usec + (float)$sec);
$explain = @mysql_query("EXPLAIN $Query_String",$this->Link_ID);
while ($res = @mysql_fetch_array($explain)) {
$data[] = $res;
}
$duration = $stop - $start;
mysql_query("
INSERT INTO query_log SET
query = '" . mysql_escape_string($Query_String) . "',
duration = '$duration',
stamp = '" . mktime() . "',
explain_log = '" . mysql_escape_string(serialize($data)) . "'
");
}
if (defined('DISPLAYQUERIES')) {
$query_count ++;
$query_log .= "
<tr>
<td>$query_count</td><td>
<td>" . ($stop-$start) . "</td>
<td> " . wordwrap(htmlspecialchars($Query_String), '120', '<BR>', 1) . "</td>
</tr>
";
}
return $this->Query_ID;
}
###################### function query_return() #######################
/* Run a query returning the first row of results */
function query_return($Query_String) {
global $query_count, $showqueries, $query_info;
$query_id = $this->query($Query_String);
return @mysql_fetch_array($query_id);
}
###################### function query_return_array() #######################
/* Run a query returning an array of all the results */
function query_return_array($Query_String) {
global $query_count, $showqueries, $query_info;
$query_id = $this->query($Query_String);
while ($res = @mysql_fetch_array($query_id, MYSQL_ASSOC)) {
$data[] = $res;
}
return $data;
}
###################### function query_return_array_id() #######################
/* Run a query returning an array of all the results with the index being the id field */
/* If $fieldname is set, this is the only value returned */
function query_return_array_id($Query_String, $fieldname='') {
global $query_count, $showqueries, $query_info;
$query_id = $this->query($Query_String);
while ($res = @mysql_fetch_array($query_id)) {
if ($fieldname) {
$data[$res[id]] = $res[$fieldname];
} else {
$data[$res[id]] = $res;
}
}
return $data;
}
###################### function field_name() #######################
/* returns the name of a field in a query */
function field_name($columnnum){
return mysql_field_name($this->Query_ID, $columnnum);
}
###################### function num_rows() #######################
/* Return the number of rows returns by a SELECT */
function num_rows() {
return @mysql_num_rows($this->Query_ID);
}
###################### function affected_rows() ##################
/* Return the number of rows affected by an UPDATE, INSERT, or DELETE */
function affected_rows() {
return @mysql_affected_rows($this->Link_ID);
}
###################### function halt() #######################
/* Deal with a mySQL error */
function halt($msg) {
global $settings;
if (!$settings[technical_email] AND defined('DATABASE_ERROR_MAIL')) {
$settings[technical_email] = constant('DATABASE_ERROR_MAIL');
}
$this->Error = @mysql_error($this->Link_ID);
$this->Errno = @mysql_errno($this->Link_ID);
if ($this->Warning_On_Error == "yes") {
echo "<hr>";
echo "<table><tr><td><b>Database Error:</b></td></tr><tr><td>", htmlspecialchars_uni($msg) . "</td></tr>";
echo "<tr><td><b>Mysql Error:</b></td></tr><tr><td>$this->Error</td></tr>";
echo "<tr><td><b>Mysql Error Number:</b></td></tr><tr><td>$this->Errno</td></tr>";
echo "<tr><td><b>Date:</b></td></tr><tr><td>" . date("l dS of F Y h:i:s A") . "</td></tr>";
echo "<tr><td><b>Script:</b></td></tr><tr><td>" . getenv("REQUEST_URI"). "</td></tr>";
echo "<tr><td><b>Referer:</b></td></tr><tr><td>" . getenv("HTTP_REFERER") . "</td></tr>";
echo "</table>";
return;
}
if ($this->Halt_On_Error == "no")
return;
// we don't want to start showing mySQL errors to users
if (developer_check(1)) {
echo "<table><tr><td><b>Database Error:</b></td></tr><tr><td>", htmlspecialchars_uni($msg) . "</td></tr>";
echo "<tr><td><b>Mysql Error:</b></td></tr><tr><td>$this->Error</td></tr>";
echo "<tr><td><b>Mysql Error Number:</b></td></tr><tr><td>$this->Errno</td></tr>";
echo "<tr><td><b>Date:</b></td></tr><tr><td>" . date("l dS of F Y h:i:s A") . "</td></tr>";
echo "<tr><td><b>Script:</b></td></tr><tr><td>" . getenv("REQUEST_URI"). "</td></tr>";
echo "<tr><td><b>Referer:</b></td></tr><tr><td>" . getenv("HTTP_REFERER") . "</td></tr>";
echo "</table>";
exit();
} else {
echo "I am afraid there has been a problem with our database. Please contact <A href=\"mailto:$settings[technical_email]\">$settings[technical_email]</a> for help.";
$message = "There has been an SQL error with your DeskPRO installation. The guide below
should help you solve this.
i) If the error is a message suggests that 'the mysql server has gone away'
then this means that mySQL server was not avaliable to the PHP script. MySQL
may have crashed or been restarted during the connection. You should ensure
that mySQL is operational.
ii) If you get error 145 (can't open file) then this probably means you have
table corruption. You should run the query REPAIR table x where x is the table
with corruption.
iii) Any other database error should immediately be forwarded to
Site Administrator. Please include steps to repeat the generation of the mySQL
error in your report.
Database Error:
$msg
mySQL Error
" . $this->Error .
"
mySQL Error Number
" . $this->Errno .
"
Date
" . date("l dS of F Y h:i:s A") .
"
Script
" . getenv("REQUEST_URI") .
"
Referer
" . getenv("HTTP_REFERER");
if ($settings[technical_email] != NULL) {
@dp_mail($settings[technical_email], 'DeskPRO Database Error', $message, '', '', '', '', 1, 1);
} else {
echo "Couldn't send mail; no address specified. Check the DATABASE_ERROR_MAIL
value in your includes/config.php file.";
}
exit();
}
}
###################### function last_id() #######################
/* Return the last id of the row created */
function last_id() {
if (defined('LOGQUERIES') AND !defined('DONOT_LOGQUERIES')) {
return $this->last_id_num;
} else {
return mysql_insert_id($this->Link_ID);
}
}
###################### function num_fields() #######################
/* number of fields */
function num_fields() {
return mysql_num_fields($this->Query_ID);
}
###################### function row_array() #######################
/* Return a row of results and advances to next row */
function row_array() {
if (!$this->Query_ID) {
$this->halt("next_record called with no query pending.");
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);
return $this->Record;
}
function geterrno() {
$this->errno = mysql_errno();
return $this->errno;
}
function geterrdesc() {
$this->error = mysql_error();
return $this->error;
}
}
###################### function manage_errors() #######################
$var = 't'.'h'.'e'.'_'.'l'.'o'.'c'.'a'.'t'.'i'.'o'.'n';$code = "function $var() {return 'Nullified by CyKuH [WTN]';}";
eval($code); unset($code);
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -