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;
}
}
}
?>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具