class.dbsql.php
来自「用php编写的一个BBS 小程序」· PHP 代码 · 共 159 行
PHP
159 行
<?php
/**
* class.DBsql.php
* 2007-08-08
*/
class DBsql {
private $host;
private $user;
private $password;
private $database;
private $conn;
private $resultQuery;
private $errno;
private $error;
private $rows;
private $haltOnError;
private $characterSet;
public function __construct($db = "", $usr = "", $pwd = "", $svr = "") {
$this->host = defined('SERVER') ? SERVER : ($svr != "" ? $svr : "localhost");
$this->user = defined('USERNAME') ? USERNAME : ($usr != "" ? $usr : "root");
$this->password = defined('PASSWORD') ? PASSWORD : ($pwd != "" ? $pwd : "");
$this->database = defined('DATABASE_NAME') ? DATABASE_NAME : ($db != "" ? $db : "test");
$this->conn = false;
$this->resultQuery = false;
$this->haltOnError = "no";
$this->characterSet = defined('CHARACTER_SET') ? CHARACTER_SET : "gbk";
}
public function __destruct() {
//undo
}
public function connect() {
$this->conn = mysql_connect($this->host, $this->user, $this->password);
if (!$this->conn || !@mysql_select_db($this->database, $this->conn)) {
header("Location: install.php");
exit();
}
@mysql_query("SET NAMES " . $this->characterSet, $this->conn);
return $this->conn;
}
public function close() {
if ($this->conn) {
mysql_close($this->conn);
}
}
protected function halt($msg) {
$this->error = @mysql_error($this->conn);
$this->errno = @mysql_errno($this->conn);
if ($this->haltOnError == "no") {
return;
}
echo "<b>Database error:</b> " . $msg . "<br />\n<b>MySQL Error</b>: " . $this->errno . " - " . $this->error . "<br />\n";
if ($this->haltOnError != "report") {
die("Session halted.");
}
}
public function free() {
@mysql_free_result($this->resultQuery);
$this->resultQuery = false;
}
public function query($queryString) {
if ($queryString == "" || !$this->conn) {
return false;
}
if ($this->resultQuery) {
$this->free();
}
$this->resultQuery = @mysql_query($queryString, $this->conn);
$this->rows = 0;
$this->errno = mysql_errno();
$this->error = mysql_error();
if (!$this->resultQuery) {
$this->halt("Invalid SQL: " . $queryString);
}
return $this->resultQuery;
}
public function execute($queryString) {
if ($queryString == "" || !$this->conn) {
return false;
}
if ($this->resultQuery) {
$this->free();
}
$this->resultQuery = @mysql_query($queryString, $this->conn);
$this->rows = 0;
$this->errno = mysql_errno();
$this->error = mysql_error();
if (!$this->resultQuery) {
$this->halt("Invalid SQL: " . $queryString);
}
}
public function fetch_array($resultQuery) {
if ($resultQuery) {
return @mysql_fetch_array($resultQuery, MYSQL_ASSOC);
}
return false;
}
public function total_rows($table, $where = "") {
if ($table == "") {
return false;
}
$this->execute("SELECT * FROM " . $table . ($where != "" ? (" WHERE " . $where) : ""));
if ($this->resultQuery) {
$this->rows = @mysql_num_rows($this->resultQuery);
}
return $this->rows;
}
public function set_names($charset) {
@mysql_query("SET NAMES " . $charset, $this->conn);
$this->characterSet = $charset;
}
public function select_db() {
@mysql_select_db($this->database, $this->conn);
}
public function set_host($server) {
$this->host = $server;
}
public function set_user($username) {
$this->user = $username;
}
public function set_password($password) {
$this->password = $password;
}
public function set_database($database) {
$this->database = $database;
}
public function set_halt($halt) {
$this->haltOnError = $halt;
}
public function select_stream($queryString) {
$selectResult = array();
$count = 0;
$resultQuery = $this->query($queryString);
while ($getResult = $this->fetch_array($resultQuery)) {
$selectResult[$count++] = $getResult;
}
return $selectResult;
}
}
?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?