zoukankan
html css js c++ java
超级简单但超级实用的 PHP 的 mysql 类
<?
php
/*
*
* @author 马秉尧
* @copyright (C) 2005 CoolCode.CN
* @package class_mysql.php
* @version 0.8
*/
class
mysql
{
var
$debug
;
var
$host
;
var
$name
;
var
$user
;
var
$pass
;
var
$linkid
;
var
$queryid
;
var
$affected_rows
;
var
$result
;
var
$sql
;
var
$errno
;
var
$error
;
var
$sqls
=
array
();
function
mysql
(
$host
=
''
,
$name
=
''
,
$user
=
''
,
$pass
=
''
,
$charset
=
'
UTF8
'
,
$debug
=
false
) {
if
(
!
extension_loaded
(
'
mysql
'
)) {
if
(
strtoupper
(
substr
(
PHP_OS
,
0
,
3
)
==
'
WIN
'
)) {
dl
(
'
php_mysql.dll
'
);
}
else
{
dl
(
'
mysql.so
'
);
}
}
$this
->
debug
=
$debug
;
if
(
$host
!=
''
) {
if
(
is_resource
(
$this
->
connect(
$host
,
$user
,
$pass
))) {
if
(
mysql_get_server_info
()
>=
"
4.1
"
) {
mysql_query
(
"
set names
$charset
"
,
$this
->
linkid);
}
if
(
$name
!=
''
) {
$this
->
select_db(
$name
);
}
}
}
register_shutdown_function
(
array
(
&
$this
,
"
shutdown
"
));
}
function
connect(
$host
,
$user
,
$pass
) {
$this
->
host
=
$host
;
$this
->
user
=
$user
;
$this
->
pass
=
$pass
;
if
(
$this
->
debug) {
$this
->
linkid
=
mysql_connect
(
$this
->
host
,
$this
->
user
,
$this
->
pass);
}
else
{
$this
->
linkid
=
@
mysql_connect
(
$this
->
host
,
$this
->
user
,
$this
->
pass);
}
$this
->
errno
=
mysql_errno
();
$this
->
error
=
mysql_error
();
return
$this
->
linkid;
}
function
select_db(
$name
) {
$this
->
name
=
$name
;
if
(
is_resource
(
$this
->
linkid)) {
if
(
$this
->
debug) {
if
(
!
mysql_select_db
(
$this
->
name
,
$this
->
linkid)) {
$this
->
errno
=
mysql_errno
(
$this
->
linkid);
$this
->
error
=
mysql_error
(
$this
->
linkid);
}
}
else
{
if
(
!
@
mysql_select_db
(
$this
->
name
,
$this
->
linkid)) {
$this
->
errno
=
mysql_errno
(
$this
->
linkid);
$this
->
error
=
mysql_error
(
$this
->
linkid);
}
}
}
}
function
query(
$sql
=
''
,
$type
=
'
assoc
'
) {
if
(
is_array
(
$sql
)) {
$err
=
false
;
foreach
(
$sql
as
$s
) {
if
(
$this
->
debug) {
if
(
!
mysql_query
(
$s
,
$this
->
linkid)) {
$this
->
errno
=
mysql_errno
(
$this
->
linkid);
$this
->
error
=
mysql_error
(
$this
->
linkid);
$err
=
true
;
}
}
else
{
if
(
!
@
mysql_query
(
$s
,
$this
->
linkid)) {
$this
->
errno
=
mysql_errno
(
$this
->
linkid);
$this
->
error
=
mysql_error
(
$this
->
linkid);
$err
=
true
;
}
}
if
(
preg_match
(
"
/^\s*(delete|insert|replace|update)/i
"
,
$s
)) {
$this
->
affected_rows
=
mysql_affected_rows
(
$this
->
linkid);
}
}
return
!
$err
;
}
if
(
$sql
!=
''
) {
$this
->
sql
=
$sql
;
}
if
(
$this
->
debug) {
$this
->
queryid
=
mysql_query
(
$this
->
sql
,
$this
->
linkid);
}
else
{
$this
->
queryid
=
@
mysql_query
(
$this
->
sql
,
$this
->
linkid);
}
if
(
preg_match
(
"
/^\s*(delete|insert|replace|update)/i
"
,
$this
->
sql)) {
$this
->
affected_rows
=
mysql_affected_rows
(
$this
->
linkid);
}
if
(
is_resource
(
$this
->
queryid)) {
if
(
mysql_num_rows
(
$this
->
queryid)
==
0
) {
$this
->
result
=
0
;
}
else
if
((
mysql_num_rows
(
$this
->
queryid)
==
1
) and
preg_match
(
"
/limit\s+1\s*$|limit\s+1\s*[^,]+/i
"
,
$this
->
sql)) {
$this
->
result
=
call_user_func
(
"
mysql_fetch_
"
.
$type
,
$this
->
queryid);
}
else
{
$this
->
result
=
array
();
while
(
$row
=
call_user_func
(
"
mysql_fetch_
"
.
$type
,
$this
->
queryid)) {
array_push
(
$this
->
result
,
$row
);
}
}
return
$this
->
result;
}
else
if
(
$this
->
queryid
===
false
) {
$this
->
errno
=
mysql_errno
(
$this
->
linkid);
$this
->
error
=
mysql_error
(
$this
->
linkid);
}
return
$this
->
queryid;
}
function
explain(
$sql
=
''
) {
if
(
$sql
!=
''
) {
$this
->
sql
=
$sql
;
}
if
(
$this
->
debug) {
$this
->
queryid
=
mysql_query
(
"
EXPLAIN
"
.
$this
->
sql
,
$this
->
linkid);
}
else
{
$this
->
queryid
=
@
mysql_query
(
"
EXPLAIN
"
.
$this
->
sql
,
$this
->
linkid);
}
if
(
is_resource
(
$this
->
queryid)) {
$this
->
result
=
array
();
while
(
$row
=
mysql_fetch_array
(
$this
->
queryid)) {
array_push
(
$this
->
result
,
$row
);
}
return
$this
->
result;
}
else
{
$this
->
errno
=
mysql_errno
(
$this
->
linkid);
$this
->
error
=
mysql_error
(
$this
->
linkid);
return
false
;
}
}
function
last_insert_id() {
if
(
$this
->
debug) {
$result
=
mysql_query
(
"
select LAST_INSERT_ID() as `last_insert_id`
"
,
$this
->
linkid);
}
else
{
$result
=
@
mysql_query
(
"
select LAST_INSERT_ID() as `last_insert_id`
"
,
$this
->
linkid);
}
if
(
is_resource
(
$result
)) {
$row
=
mysql_fetch_object
(
$result
);
return
$row
->
last_insert_id;
}
else
{
$this
->
errno
=
mysql_errno
(
$this
->
linkid);
$this
->
error
=
mysql_error
(
$this
->
linkid);
return
false
;
}
}
function
affected_rows() {
return
$this
->
affected_rows;
}
function
register_shutdown_query(
$sql
) {
array_push
(
$this
->
sqls
,
$sql
);
}
function
shutdown() {
if
(
is_resource
(
$this
->
linkid)) {
foreach
(
$this
->
sqls
as
$sql
) {
if
(
$this
->
debug) {
mysql_query
(
$sql
,
$this
->
linkid);
}
else
{
@
mysql_query
(
$sql
,
$this
->
linkid);
}
}
}
}
function
close() {
if
(
$this
->
debug) {
mysql_close
(
$this
->
linkid);
}
else
{
@
mysql_close
(
$this
->
linkid);
}
}
function
escape_string(
$string
) {
if
(
version_compare
(
phpversion
()
,
"
4.3.0
"
)
==
"
-1
"
) {
return
mysql_escape_string
(
$string
);
}
else
if
(
is_resource
(
$this
->
linkid)) {
return
mysql_real_escape_string
(
$string
,
$this
->
linkid);
}
else
{
return
false
;
}
}
}
?>
查看全文
相关阅读:
对称的二叉树
二叉树的下一个结点
Go操作Redis实战
重写Laravel异常处理类
【论文笔记】Learning to Estimate 3D Human Pose and Shape from a Single Color Image(CVPR 2018)
ffmpeg第一弹:ffmpeg介绍和开发环境搭建
程序员你是如何使用镜像中心Harbor的?
SpringBoot 的 MyBatis 多数据源配置
Typora+PicGo+Gitee搭建博客写作环境(超详细)
重学数据结构(八、查找)
原文地址:https://www.cnblogs.com/studio313/p/1061292.html
最新文章
小程序云开发持续交付和质量管控(下)
小程序云开发持续交付和质量管控(上)
1分钟部署一个属于自己的网站,借助云开发静态网站部署属于自己的网站
DarkMode(5):深色模式不同实现方案切换
DarkMode(4):css滤镜 颜色反转实现深色模式
DarkMode(3):sass函数实实现深色模式操作
DarkMode(2):深色模式解决方案——css颜色变量实现Dark Mode
DarkMode(1):产品应用深色模式分析
傲视Kubernetes(四):Pod的创建及标签的使用
傲视Kubernetes(三):Kubernetes中的Pod
热门文章
傲视Kubernetes(二):Docker镜像搭建与本地Kubernetes环境搭建
解决Windows平台下MySQL8.0导入表后表名变成小写问题
二叉搜索树的第 k 个结点
序列化二叉树
把二叉树打印成多行
JVM HotSpot 可达性分析算法实现细节
JVM 经典垃圾收集器 —— CMS 收集器和 G1 收集器
JVM 经典垃圾收集器
Java 面向对象概述
按之字形顺序打印二叉树
Copyright © 2011-2022 走看看