php封装数据库连接类,php接口封装
php封装一个class类,实现mysql数据库的增删改查怎么操做?
class sqlHelper{ \x0d\x0a public $conn; \x0d\x0a public $dbname="数据库名称"; \x0d\x0a public $username="数据库用户名"; \x0d\x0a public $password="数据库密码"; \x0d\x0a public $host="localhost"; \x0d\x0a //连接数据库 \x0d\x0a public function __construct(){ \x0d\x0a $this-conn=mysql_connect($this-host,$this-username,$this-password); \x0d\x0a if(!$this-conn){ \x0d\x0a die("连接失败".mysql_error()); \x0d\x0a } \x0d\x0a mysql_select_db($this-dbname,$this-conn); \x0d\x0a } \x0d\x0a //执行查询语句 \x0d\x0a public function execute_dql($sql){ \x0d\x0a $res=mysql_query($sql,$this-conn); \x0d\x0a return $res; \x0d\x0a } \x0d\x0a //执行增填改语句 \x0d\x0a public function execute_dml($sql){ \x0d\x0a $b=mysql_query($sql,$this-conn); \x0d\x0a if(!$b){ \x0d\x0a return 3; \x0d\x0a }else{ \x0d\x0a if(mysql_affected_rows($this-conn)){ \x0d\x0a return 1;//表示OK \x0d\x0a }else{ \x0d\x0a return 2;//表示没有行收到影响 \x0d\x0a } \x0d\x0a } \x0d\x0a }\x0d\x0a}
成都创新互联公司是一家专业提供梅里斯企业网站建设,专注与网站建设、成都网站建设、H5技术、小程序制作等业务。10年已为梅里斯众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。
PHP访问MYSQL数据库封装类(附函数说明)
复制代码
代码如下:
?php
/*
MYSQL
数据库访问封装类
MYSQL
数据访问方式,php4支持以mysql_开头的过程访问方式,php5开始支持以mysqli_开头的过程和mysqli面向对象
访问方式,本封装类以mysql_封装
数据访问的一般流程:
1,连接数据库
mysql_connect
or
mysql_pconnect
2,选择数据库
mysql_select_db
3,执行SQL查询
mysql_query
4,处理返回的数据
mysql_fetch_array
mysql_num_rows
mysql_fetch_assoc
mysql_fetch_row
etc
*/
class
db_mysql
{
var
$querynum
=
;
//当前页面进程查询数据库的次数
var
$dblink
;
//数据库连接资源
//链接数据库
function
connect($dbhost,$dbuser,$dbpw,$dbname='',$dbcharset='utf-8',$pconnect=0
,
$halt=true)
{
$func
=
empty($pconnect)
?
'mysql_connect'
:
'mysql_pconnect'
;
$this-dblink
=
@$func($dbhost,$dbuser,$dbpw)
;
if
($halt
!$this-dblink)
{
$this-halt("无法链接数据库!");
}
//设置查询字符集
mysql_query("SET
character_set_connection={$dbcharset},character_set_results={$dbcharset},character_set_client=binary",$this-dblink)
;
//选择数据库
$dbname
@mysql_select_db($dbname,$this-dblink)
;
}
//选择数据库
function
select_db($dbname)
{
return
mysql_select_db($dbname,$this-dblink);
}
//执行SQL查询
function
query($sql)
{
$this-querynum++
;
return
mysql_query($sql,$this-dblink)
;
}
//返回最近一次与连接句柄关联的INSERT,UPDATE
或DELETE
查询所影响的记录行数
function
affected_rows()
{
return
mysql_affected_rows($this-dblink)
;
}
//取得结果集中行的数目,只对select查询的结果集有效
function
num_rows($result)
{
return
mysql_num_rows($result)
;
}
//获得单格的查询结果
function
result($result,$row=0)
{
return
mysql_result($result,$row)
;
}
//取得上一步
INSERT
操作产生的
ID,只对表有AUTO_INCREMENT
ID的操作有效
function
insert_id()
{
return
($id
=
mysql_insert_id($this-dblink))
=
?
$id
:
$this-result($this-query("SELECT
last_insert_id()"),
0);
}
//从结果集提取当前行,以数字为key表示的关联数组形式返回
function
fetch_row($result)
{
return
mysql_fetch_row($result)
;
}
//从结果集提取当前行,以字段名为key表示的关联数组形式返回
function
fetch_assoc($result)
{
return
mysql_fetch_assoc($result);
}
//从结果集提取当前行,以字段名和数字为key表示的关联数组形式返回
function
fetch_array($result)
{
return
mysql_fetch_array($result);
}
//关闭链接
function
close()
{
return
mysql_close($this-dblink)
;
}
//输出简单的错误html提示信息并终止程序
function
halt($msg)
{
$message
=
"html\nhead\n"
;
$message
.=
"meta
content='text/html;charset=gb2312'\n"
;
$message
.=
"/head\n"
;
$message
.=
"body\n"
;
$message
.=
"数据库出错:".htmlspecialchars($msg)."\n"
;
$message
.=
"/body\n"
;
$message
.=
"/html"
;
echo
$message
;
exit
;
}
}
?
php 连接数据库类
我也刚刚学PHP,正在研究中,虽然你只给10分........
首先,将代码保存到一个文件,如:mysql.class.php
其次,在一个常用的文件里调用:比如头部文件header.php,因为我放在根目录所以用下面方式导入其他文件:
require dirname(__FILE__) . 'include/config.php';
//导入类文件
require dirname(__FILE__) . 'include/mysql.class.php';
//定义一个类及初始化数据库类
$db = new mysql($db_host, $db_user, $db_pass, $db_name);
$db_host = $db_user = $db_pass = $db_name = NULL;
然后,在test.php文件调用:
require_once dirname(__FILE__) . '/header.php';
使用方法:
$sql = "读取表";
$res = $db-query($sql);
$info = array();//定义数组
while($row=$db-fetchRow($res))
{
$arr['id'] = $row['id'];
$arr['title'] = $row['title'];
$info[] = $arr;
}
可以在显示的地方用:
foreach($info as $i)
{
echo $i['title']."br /";
}
或是直接使用while
还用另一种调用方式:
$here_area = $db-getRow("select areaid,areaname from {$table}area where areaid='$areaid'");
$here[] = array('name'=$here_area['areaname'],'id'=$here_area['areaid']);
测试通过,因为我正在使用.....................................
config.php代码:
?php
$db_host = "localhost";
$db_name = "test";
$db_user = "root";
$db_pass = "";
$table = "mini_";
$charset = "gb2312";
$dbcharset = "gbk";
?
mysql.class.php代码:
?php
class mysql
{
var $link = NULL;
//自动执行__construct php5类构建方法,如果PHP4和PHP5同时使用会自动使用PHP5的方法
function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)
{
//自动执行时调用mysql函数
$this-mysql($dbhost, $dbuser, $dbpw, $dbname, $pconnect, $quiet);
}
//php4类构建方法,如果没有 __construct 就自动执行此功能
function mysql($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)
{
if ($quiet)
{
$this-connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, $quiet);
}
else
{
$this-settings = array(
'dbhost' = $dbhost,
'dbuser' = $dbuser,
'dbpw' = $dbpw,
'dbname' = $dbname,
'charset' = $charset,
'pconnect' = $pconnect
);
}
}
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)
{
global $dbcharset;
if ($pconnect)
{
if (!($this-link = @mysql_pconnect($dbhost, $dbuser, $dbpw)))
{
if (!$quiet)
{
$this-ErrorMsg("Can't pConnect MySQL Server($dbhost)!");
}
return false;
}
}
else
{
if (PHP_VERSION = '4.2')
{
$this-link = @mysql_connect($dbhost, $dbuser, $dbpw, true);
}
else
{
$this-link = @mysql_connect($dbhost, $dbuser, $dbpw);
mt_srand((double)microtime() * 1000000);
}
if (!$this-link)
{
if (!$quiet)
{
$this-ErrorMsg("Can't Connect MySQL Server($dbhost)!");
}
return false;
}
}
$this-dbhash = md5($this-root_path . $dbhost . $dbuser . $dbpw . $dbname);
$this-version = mysql_get_server_info($this-link);
if ($this-version '4.1')
{
if ($dbcharset != 'latin1')
{
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this-link);
}
if ($this-version '5.0.1')
{
mysql_query("SET sql_mode=''", $this-link);
}
}
if ($dbname)
{
if (mysql_select_db($dbname, $this-link) === false )
{
if (!$quiet)
{
$this-ErrorMsg("Can't select MySQL database($dbname)!");
}
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
function query($sql, $type = '')
{
if ($this-link === NULL)
{
$this-connect($this-settings['dbhost'], $this-settings['dbuser'], $this-settings['dbpw'], $this-settings['dbname'], $this-settings['charset'], $this-settings['pconnect']);
$this-settings = array();
}
if ($this-queryCount++ = 99)
{
$this-queryLog[] = $sql;
}
if ($this-queryTime == '')
{
if (PHP_VERSION = '5.0.0')
{
$this-queryTime = microtime(true);
}
else
{
$this-queryTime = microtime();
}
}
if (!($query = mysql_query($sql, $this-link)) $type != 'SILENT')
{
$this-error_message[]['message'] = 'MySQL Query Error';
$this-error_message[]['sql'] = $sql;
$this-error_message[]['error'] = mysql_error($this-link);
$this-error_message[]['errno'] = mysql_errno($this-link);
$this-ErrorMsg();
return false;
}
return $query;
}
function affected_rows()
{
return mysql_affected_rows($this-link);
}
function num_fields($query)
{
return mysql_num_fields($query);
}
function error()
{
return mysql_error($this-link);
}
function errno()
{
return mysql_errno($this-link);
}
function num_rows($query)
{
return mysql_num_rows($query);
}
function insert_id()
{
return mysql_insert_id($this-link);
}
function fetchRow($query)
{
return mysql_fetch_assoc($query);
}
function fetcharray($query)
{
return mysql_fetch_array($query);
}
function version()
{
return $this-version;
}
function close()
{
return mysql_close($this-link);
}
function ErrorMsg($message = '', $sql = '')
{
if ($message)
{
echo "$message\n\n";
}
else
{
echo "bMySQL server error report:";
print_r($this-error_message);
}
exit;
}
function getCol($sql)
{
$res = $this-query($sql);
if ($res !== false)
{
$arr = array();
while ($row = mysql_fetch_row($res))
{
$arr[] = $row[0];
}
return $arr;
}
else
{
return false;
}
}
function getOne($sql, $limited = false)
{
if ($limited == true)
{
$sql = trim($sql . ' LIMIT 1');
}
$res = $this-query($sql);
if ($res !== false)
{
$row = mysql_fetch_row($res);
if ($row !== false)
{
return $row[0];
}
else
{
return '';
}
}
else
{
return false;
}
}
function getAll($sql)
{
$res = $this-query($sql);
if ($res !== false)
{
$arr = array();
while ($row = mysql_fetch_assoc($res))
{
$arr[] = $row;
}
return $arr;
}
else
{
return false;
}
}
//使用: getRow($sql,true) 如果有true那值是 limit 1,读取一条信息
function getRow($sql, $limited = false)
{
if ($limited == true)
{
$sql = trim($sql . ' LIMIT 1');
}
$res = $this-query($sql);
if ($res !== false)
{
return mysql_fetch_assoc($res);
}
else
{
return false;
}
}
}
?
标题名称:php封装数据库连接类,php接口封装
转载来源:http://azwzsj.com/article/heeeeg.html