php 数据库mysql查询与连接类
class mysql
{
var $host = ""; //mysql主机名
var $user = ""; //mysql用户名
var $pwd = ""; //mysql密码
var $dbName = ""; //mysql数据库名称
var $linkID = 0; //用来保存连接ID
var $queryID = 0; //用来保存查询ID
var $fetchMode= MYSQL_ASSOC;//取记录时的模式
var $queryTimes = 0; //保存查询的次数
var $errno = 0; //mysql出错代号
var $error = ""; //mysql出错信息
var $record = array(); //一条记录数组
//======================================
// 函数: mysql()
// 功能: 构造函数
// 参数: 参数类的变量定义
// 说明: 构造函数将自动连接数据库
// 如果想手动连接去掉连接函数
//======================================
function mysql($host,$user,$pwd,$dbName)
{ if(empty($host) || empty($user) || empty($dbName))
$this->halt("数据库主机地址,用户名或数据库名称不完全,请检查!");
$this->host = $host;
$this->user = $user;
$this->pwd = $pwd;
$this->dbName = $dbName;
$this->connect();//设置为自动连接
}
//======================================
// 函数: connect($host,$user,$pwd,$dbName)
// 功能: 连接数据库
// 参数: $host 主机名, $user 用户名
// 参数: $pwd 密码, $dbName 数据库名称
// 返回: 0:失败
// 说明: 默认使用类中变量的初始值
//======================================
function connect($host = "", $user = "", $pwd = "", $dbName = "")
{
if ("" == $host)
$host = $this->host;
if ("" == $user)
$user = $this->user;
if ("" == $pwd)
$pwd = $this->pwd;
if ("" == $dbName)
$dbName = $this->dbName;
//now connect to the database
$this->linkID = mysql_pconnect($host, $user, $pwd);
if (!$this->linkID)
{
$this->halt();
return 0;
}
if (!mysql_select_db($dbName, $this->linkID))
{
$this->halt();
return 0;
}
return $this->linkID;
}
//======================================
// 函数: query($sql)
// 功能: 数据查询
// 参数: $sql 要查询的SQL语句
// 返回: 0:失败
//======================================
function query($sql)
{
$this->queryTimes++;
$this->queryID = mysql_query($sql, $this->linkID);
if (!$this->queryID)
{
$this->halt();
return 0;
}
return $this->queryID;
}
//======================================
// 函数: setFetchMode($mode)
// 功能: 设置取得记录的模式
// 参数: $mode 模式 MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
// 返回: 0:失败
//======================================
function setFetchMode($mode)
{
if ($mode == MYSQL_ASSOC || $mode == MYSQL_NUM || $mode == MYSQL_BOTH)
{
$this->fetchMode = $mode;
return 1;
}
else
{
$this->halt("错误的模式.");
return 0;
}
}
//======================================
// 函数: fetchRow()
// 功能: 从记录集中取出一条记录
// 返回: 0: 出错 record: 一条记录
//======================================
function fetchRow()
{
$this->record = mysql_fetch_array($this->queryID,$this->fetchMode);
return $this->record;
}
//======================================
// 函数: fetchAll()
// 功能: 从记录集中取出所有记录
// 返回: 记录集数组
//======================================
function fetchAll()
{
$arr = array();
while($this->record = mysql_fetch_array($this->queryID,$this->fetchMode))
{
$arr[] = $this->record;
}
mysql_free_result($this->queryID);
return $arr;
}
//======================================
// 函数: getValue()
// 功能: 返回记录中指定字段的数据
// 参数: $field 字段名或字段索引
// 返回: 指定字段的值
//======================================
function getValue($field)
{
return $this->record[$field];
}
//======================================
// 函数: affectedRows()
// 功能: 返回影响的记录数
//======================================
function affectedRows()
{
return mysql_affected_rows($this->linkID);
}
//======================================
// 函数: recordCount()
// 功能: 返回查询记录的总数
// 参数: 无
// 返回: 记录总数
//======================================
function recordCount()
{
return mysql_num_rows($this->queryID);
}
//======================================
// 函数: getQueryTimes()
// 功能: 返回查询的次数
// 参数: 无
// 返回: 查询的次数
//======================================
function getQueryTimes()
{
return $this->queryTimes;
}
//======================================
// 函数: getVersion()
// 功能: 返回mysql的版本
// 参数: 无
//======================================
function getVersion()
{
$this->query("select version() as ver");
$this->fetchRow();
return $this->getValue("ver");
}
//======================================
// 函数: getDBSize($dbName, $tblPrefix=null)
// 功能: 返回数据库占用空间大小
// 参数: $dbName 数据库名
// 参数: $tblPrefix 表的前缀,可选
//======================================
function getDBSize($dbName, $tblPrefix=null)
{
$sql = "SHOW TABLE STATUS FROM " . $dbName;
if($tblPrefix != null) {
$sql .= " LIKE '$tblPrefix%'";
}
$this->query($sql);
$size = 0;
while($this->fetchRow())
$size += $this->getValue("Data_length") + $this->getValue("Index_length");
return $size;
}
//======================================
// 函数: insertID()
// 功能: 返回最后一次插入的自增ID
// 参数: 无
//======================================
function insertID() {
return mysql_insert_id();
}
//======================================
// 函数: halt($err_msg)
// 功能: 处理所有出错信息
// 参数: $err_msg 自定义的出错信息
//=====================================
function halt($err_msg="")
{
if ("" == $err_msg)
{
$this->errno = mysql_errno();
$this->error = mysql_error();
echo "mysql error:
";
echo $this->errno.":".$this->error."
";
exit;
}
else
{
echo "mysql error:
";
echo $err_msg."
";
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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.
