php数据库连接接口 php连接数据库有什么用( 四 )


$userArr= array(
'username' = 'hohoho',
'passwd' = '1474rr4448522',
'email' = 'qsf.rrza@165.com',
'sex' = '0',
'desc' = '厨师qq',
);
$affectedRows = $userObj-update($userArr, $insetId);
if ($affectedRows0) {
echo "影响行数为:{$affectedRows}BR";
}
//----------------------------------------查询数据----------------------------------------------
$userRs0 = $userObj-select(8);//单个主键值
//print_r($userRs0);
$userRs1 = $userObj-select(array(1,5,8));//多个主键值的数组
//print_r($userRs1);
$userRs2 = $userObj-select('select count(*) as count fromuser where uid20');//直接完整sql语句
//print_r($userRs2);
$userRs3 = $userObj-select("`uid`0");//where条件
//print_r($userRs3);
$userRs4 = $userObj-getOne("`uid`0"); //获取单条记录
//print_r($userRs4);
$usersRs5 = $userObj-getAll("`uid`0"); ////获取所有记录
//print_r($usersRs5);
$usersRs6 = $userObj-limit('0,10')-where('uid100')-order('uid DESC')-group('username')-select();
//print_r($usersRs6);
//----------------------------------------删除数据-----------------------------------------------
//删除操作传递参数的方式和select操作一样
$userObj-delete(60);//单个主键值
$userObj-delete(array(1,5,8));//多个主键值的数组
$userObj-delete('deletefromuser where uid100');//直接完整sql语句
$userObj-delete("`uid`100");//where条件
$userObj-limit('5')-where('uid80')-delete();
//----------------------------------------特殊查询-----------------------------------------------
$userShowRs = $userObj-show('show create table user', true);//获取特殊查询的结果,第二个参数代表返回一条结果还是所有的结果
PHP网站怎么连接到数据库?常规方式
常规方式就是按部就班的读取文件了 。其余的话和上述方案一致 。
// 读取配置文件内容
$handle = fopen("filepath", "r");$content = fread($handle, filesize("filepath"));123
PHP解析XML
上述两种读取文件php数据库连接接口,其实都是为了PHP解析XML来做准备的 。关于PHP解析XML的方式的博客有很多 。方式也有很多php数据库连接接口 , 像simplexml , XMLReader,DOM啦等等 。但是对于比较小型的xml配置文件,simplexml就足够了 。
配置文件
?xml version="1.0" encoding="UTF-8" ?mysql
!-- 为防止出现意外,请按照此标准顺序书写.其实也无所谓了 --
hostlocalhost/host
userroot/user
password123456/password
dbtest/db
port3306/port/mysql12345678910
解析
?php/**
* 作为解析XML配置文件必备工具
*/class XMLUtil {
public static $dbconfigpath = "./db.config.xml";public static function getDBConfiguration() {
$dbconfig = array ();try {// 读取配置文件内容
$handle = fopen(self::$dbconfigpath, "r");$content = fread($handle, filesize(self::$dbconfigpath));// 获取xml文档根节点,进而获取相关的数据库信息
$mysql = simplexml_load_string($content);// 将获取到的xml节点信息赋值给关联数组,方便接下来的方法调用
$dbconfig['host'] = $mysql-host;$dbconfig['user'] = $mysql-user;$dbconfig['password'] = $mysql-password;$dbconfig['db'] = $mysql-db;$dbconfig['port'] = $mysql-port;// 将配置信息以关联数组的形式返回
return $dbconfig;
} catch ( Exception $e ) {throw new RuntimeException ( "mark读取数据库配置文件信息出错!/markbr /" );
}return $dbconfig;
}
}1234567891011121314151617181920212223242526272829
数据库连接池
对于PHP程序而言 , 优化永无止境 。而数据库连接池就在一定程度上起到了优化的作用 。其使得对用户的每一个请求而言,无需每次都像数据库申请链接资源 。而是通过已存在的数据库连接池中的链接来返回,从时间上,效率上,都是一个大大的提升 。

推荐阅读