php mysql数据库操作类(实例讲解)

2019-05-01 05:02:09丽君

实例化类:

<?php

//包含数据库操作类文件
include 'mysql.class.php';

//设置传入参数
$hostname='localhost';
$username='root';
$password='123456';
$dbname='aisi';
$charset = 'utf8';

//实例化对象

$db = new Mysql($hostname,$username,$password,$dbname);

//获取一条数据

$sql = "SELECT count(as_article_id) as count FROM as_article where as_article_type_id=1";
$count = $db->getOne($sql);

//获取多条数据

$sql = "SELECT * FROM as_article where as_article_type_id=1 order by as_article_addtime desc limit $start,$limit";
$service = $db->getAll($sql);

//插入数据

$arr = array(
'as_article_title'=>'数据库操作类',
'as_article_author'=>'rex',
);
$res = $db->insert('as_article',$arr);

//更新数据

$arr = array(
'as_article_title'=>'实例化对象',
'as_article_author'=>'Lee',
);
$where = "as_article_id=1";
$res = $db->update('as_article',$arr,$where);

//删除数据

$where = "as_article_id=1";
$res = $db->del('as_article',$where);

?>

演示完代码,大概说几句。

  getOne方法传入$sql的sql语句用于查询单条数据并返回一维数组;getAll方法同样传入sql语句,用于查询多条数据,并返回二维数组;insert方法传入表名和关联数组,返回boolen型或者插入数据对应索引;update方法传入表名、关联数组和条件,返回boolen或者影响的行数;del方法传入表名和条件,返回boolen型。

  that's all,but not the all.有兴趣的朋友可以把getOne和getAll直接传入sql语句作为参数的方式再优化一下。

相关文章 大家在看