php下mysql数据库操作类(改自discuz)
这是php mysql的数据库操作类,取自discuz,经过修改后可以用于其它程序了,并且有足够的注释,大家可以借鉴研究下.
代码如下:
/* --------------------------------
System:PT book - PT小说小偷
Code: 杰少Pakey
----------------------------------- */
$pt_mysql = new dbQuery;
/**
* mysql查询类
*
*/
class dbQuery {
/**
* 查询总次数
*
* @var int
*/
var $querynum = 0;
/**
* 连接句柄
*
* @var object
*/
var $link;
/**
* 构造函数
*
* @param string $dbhost 主机名
* @param string $dbuser 用户
* @param string $dbpw 密码
* @param string $dbname 数据库名
* @param int $pconnect 是否持续连接
*/
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0) {
if($pconnect) {
if(!$this->link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) {
$this->halt('Can not connect to MySQL server');
}
} else {
if(!$this->link = @mysql_connect($dbhost, $dbuser, $dbpw)) {
$this->halt('Can not connect to MySQL server');
}
}
if($this->version() > '4.1') {
global $dbcharset;
if($dbcharset) {
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this->link);
mysql_query("set names 'GBK'");
}
if($this->version() > '5.0.1') {
mysql_query("SET sql_mode=''", $this->link);
mysql_query("set names 'GBK'");
}
}
if($dbname) {
mysql_select_db($dbname, $this->link);
}
}
/**
* 选择数据库
*
* @param string $dbname
* @return
*/
function select_db($dbname) {
return mysql_select_db($dbname, $this->link);
}
/**
* 取出结果集中一条记录
*
* @param object $query
* @param int $result_type
* @return array
*/
function fetch_array($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}
/**
* 查询SQL
*
* @param string $sql
* @param string $type
* @return object
*/
function query($sql, $type = '') {
$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
'mysql_unbuffered_query' : 'mysql_query';
if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
$this->halt('MySQL Query Error', $sql);
}
$this->querynum++;
return $query;
}
/**
* 取影响条数
*
* @return int
*/
function affected_rows() {
return mysql_affected_rows($this->link);
}
/**
* 返回错误信息
*
* @return array
*/
function error() {
return (($this->link) ? mysql_error($this->link) : mysql_error());
}
/**
* 返回错误代码
*
* @return int
*/
function errno() {
return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
}
/**
* 返回查询结果
*
* @param object $query
* @param string $row
* @return mixed
*/
function result($query, $row) {
$query = @mysql_result($query, $row);
return $query;
}
/**
* 结果条数
*
* @param object $query
* @return int
*/
function num_rows($query) {
$query = mysql_num_rows($query);
return $query;
}
/**
* 取字段总数
*
* @param object $query
* @return int
*/
function num_fields($query) {
return mysql_num_fields($query);
}
/**
* 释放结果集
*
* @param object $query
* @return bool
*/
function free_result($query) {
return mysql_free_result($query);
}
/**
* 返回自增ID
*
* @return int
*/
function insert_id() {
return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
}
/**
* 从结果集中取得一行作为枚举数组
*
* @param object $query
* @return array
*/
function fetch_row($query) {
$query = mysql_fetch_row($query);
return $query;
}
/**
* 从结果集中取得列信息并作为对象返回
*
* @param object $query
* @return object
*/
function fetch_fields($query) {
return mysql_fetch_field($query);
}
/**
* 返回mysql版本
*
* @return string
*/
function version() {
return mysql_get_server_info($this->link);
}
/**
* 关闭连接
*
* @return bool
*/
function close() {
return mysql_close($this->link);
}
/**
* 输出错误信息
*
* @param string $message
* @param string $sql
*/
function halt($message = '', $sql = '') {
echo $message . ' ' . $sql;
exit;
}
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

View the MySQL database with the following command: Connect to the server: mysql -u Username -p Password Run SHOW DATABASES; Command to get all existing databases Select database: USE database name; View table: SHOW TABLES; View table structure: DESCRIBE table name; View data: SELECT * FROM table name;

Navicat for MariaDB cannot view the database password directly because the password is stored in encrypted form. To ensure the database security, there are three ways to reset your password: reset your password through Navicat and set a complex password. View the configuration file (not recommended, high risk). Use system command line tools (not recommended, you need to be proficient in command line tools).

Copying a table in MySQL requires creating new tables, inserting data, setting foreign keys, copying indexes, triggers, stored procedures, and functions. The specific steps include: creating a new table with the same structure. Insert data from the original table into a new table. Set the same foreign key constraint (if the original table has one). Create the same index. Create the same trigger (if the original table has one). Create the same stored procedure or function (if the original table is used).

Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).
