大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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}
创新互联服务项目包括榆树网站建设、榆树网站制作、榆树网页制作以及榆树网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,榆树网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到榆树省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
本文实例讲述了PHP基于单例模式实现的数据库操作基类。分享给大家供大家参考,具体如下:
配置文件:
?php
$db
=
array(
'host'='localhost',
'user'='root',
'password'='',
'database'='test',
)
?
php
数据库基类:
?php
class
db
{
public
$conn;
public
static
$sql;
public
static
$instance=null;
private
function
__construct(){
require_once('db.config.php');
$this-conn
=
mysql_connect($db['host'],$db['user'],$db['password']);
if(!mysql_select_db($db['database'],$this-conn)){
echo
"失败";
};
mysql_query('set
names
utf8',$this-conn);
}
public
static
function
getInstance(){
if(is_null(self::$instance)){
self::$instance
=
new
db;
}
return
self::$instance;
}
/**
*
查询数据库
*/
public
function
select($table,$condition=array(),$field
=
array()){
$where='';
if(!empty($condition)){
foreach($condition
as
$k=$v){
$where.=$k."='".$v."'
and
";
}
$where='where
'.$where
.'1=1';
}
$fieldstr
=
'';
if(!empty($field)){
foreach($field
as
$k=$v){
$fieldstr.=
$v.',';
}
$fieldstr
=
rtrim($fieldstr,',');
}else{
$fieldstr
=
'*';
}
self::$sql
=
"select
{$fieldstr}
from
{$table}
{$where}";
$result=mysql_query(self::$sql,$this-conn);
$resuleRow
=
array();
$i
=
0;
while($row=mysql_fetch_assoc($result)){
foreach($row
as
$k=$v){
$resuleRow[$i][$k]
=
$v;
}
$i++;
}
return
$resuleRow;
}
/**
*
添加一条记录
*/
public
function
insert($table,$data){
$values
=
'';
$datas
=
'';
foreach($data
as
$k=$v){
$values.=$k.',';
$datas.="'$v'".',';
}
$values
=
rtrim($values,',');
$datas
=
rtrim($datas,',');
self::$sql
=
"INSERT
INTO
{$table}
({$values})
VALUES
({$datas})";
if(mysql_query(self::$sql)){
return
mysql_insert_id();
}else{
return
false;
};
}
/**
*
修改一条记录
*/
public
function
update($table,$data,$condition=array()){
$where='';
if(!empty($condition)){
foreach($condition
as
$k=$v){
$where.=$k."='".$v."'
and
";
}
$where='where
'.$where
.'1=1';
}
$updatastr
=
'';
if(!empty($data)){
foreach($data
as
$k=$v){
$updatastr.=
$k."='".$v."',";
}
$updatastr
=
'set
'.rtrim($updatastr,',');
}
self::$sql
=
"update
{$table}
{$updatastr}
{$where}";
return
mysql_query(self::$sql);
}
/**
*
删除记录
*/
public
function
delete($table,$condition){
$where='';
if(!empty($condition)){
foreach($condition
as
$k=$v){
$where.=$k."='".$v."'
and
";
}
$where='where
'.$where
.'1=1';
}
self::$sql
=
"delete
from
{$table}
{$where}";
return
mysql_query(self::$sql);
}
public
static
function
getLastSql(){
echo
self::$sql;
}
}
$db
=
db::getInstance();
//$list
=
$db-select('demo',array('name'='tom','password'='ds'),array('name','password'));
//echo
$db-insert('demo',array('name'='脚本之家','password'='123'));
//echo
$db-update('demo',array("name"='xxx',"password"='123'),array('id'=1));
echo
$db-delete('demo',array('id'='2'));
db::getLastSql();
echo
"pre";
?
更多关于PHP操作数据库相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《PHP基于pdo操作数据库技巧总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
我也不是老手,,呵
首先,,数据库配置信息,,dbhost,dbport,dbuser,dbpass,dbname,charset这些参数最好不要设成全局变量,而从构造函数传递...
这样做的好处有几点
这个类可以单独调用,, 不用再包含配置文件,,因为你调用类的php文件一定会先包含配置文件,,再包含数据库操作类,,
通过参数传递可以提高类的独立性,,这样,,以后这个类可以被移植到任何系统里面调用,,,
1、取得结果集中字段的数目
这个是由你select 后面的东西来决定的,,如果你用的是select *
你已经写了这个
$result=mysql_query($str." limit ".$rows)or die(mysql_error());
$count=0;
$data=array();
while($rs=mysql_fetch_row($result)){
$data[$count]=$rs;
$count++;
}
@mysql_free_result($result);
return $result;
你可以在这段代码@mysql_free_result($result);之前,,用count($data[0])函数来提取,,,你这里的return $result是什么意思,,不是已经释放了吗,,应该是return $data才对
$result=mysql_query($str." limit ".$rows)or die(mysql_error());
这一句你是限制提取条数,,,但这在实际工作中没有什么用处,,,
一般的分页语句都是写在sql里面的limit x,xx;这样
你这样写,,如果有1W条记录,,你就没办法从数据库的角度去分类
第二个也是一样的
因为你的SelectRows($str,$rows)返回的是一个二维数组,,所以要知道有多少条记录,,,只要用count($data)就可以知道..
$db=new mysqlconn();
$str="SELECT * FROM xxx ORDER BY XXX ASC";
$data=$db-SelectRows($str,$rows);
$counts=count($data);//这就是取得的总记录数
我也刚刚学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;
}
}
}
?