一个php调用数据库的类
php中文网
发布时间:2016-07-25 09:10:41
|
1204人浏览过
|
来源于php中文网
原创
-
-
- class database
- {
- var $pconnect=false;//是否使用长连接
- var $mhost;//数据库主机
- var $mdatabase;
- var $db; //数据库
- var $muser;//数据库用户名
- var $mpwd;//数据库用户密码
- var $mconn;//连接标识
- var $result;// 执行query命令的结果资源标识
- var $num_rows;// 返回的条目数
- var $insert_id;// 传回最后一次使用 insert 指令的 id
- var $affected_rows;// 传回query命令所影响的列数目
- // insert、update 或 delete 所影响的列 (row) 数目。
- // delete 如果不带where,那么则返回0
- //构造函数
- public function __construct($host,$user,$pwd,$db)
- {
- $this->mhost=$host;
- $this->muser=$user;
- $this->mpwd=$pwd;
- $this->db=$db;
- }
-
- //数据库连接
- public function connect()
- {
- if($this->pconnect)
- $this->mconn=mysql_pconnect($this->mhost,$this->muser,$this->mpwd);//长连接
- else
- $this->mconn=mysql_connect($this->mhost,$this->muser,$this->mpwd);//short connect
if(!$this->mConn) $this->dbhalt("不能连接数据库!");
- if($this->db=="") $this->db=$this->dbDatabase;
-
- if(!mysql_select_db($this->db,$this->mConn))
- $this->dbhalt("数据库不可用!");
- } // eof#dbconnect()
//更改数据库
- public function dbChange($db){
- $this->db=$db;
- $this->connect();
- }
//执行SQL语句,返回结果资源id
- public function execute($sql){
- $this->result=mysql_query($sql);
- return $this->result;
- }
//获取数组-索引和关联
- public function fetchArray($resultType=MYSQL_BOTH)
- {
- return mysql_fetch_array($this->result,$resultType);
- }
-
- //获取关联数组
- public function fetchAssoc()
- {
- return mysql_fetch_assoc($this->result);
- }
-
- //获取数字索引数组
- public function fetchIndexArray()
- {
- return mysql_fetch_row($this->result);
- }
-
- //获取对象数组
- public function fetchObject()
- {
- return mysql_fetch_object($this->result);
- }
-
- //返回记录行数
- function numRows()
- {
- return mysql_num_rows($this->result);
- }
立即学习“PHP免费学习笔记(深入)”;
//返回主机中所有数据库名
- public function dbNames()
- {
- $rsPtr=mysql_list_dbs($this->mConn);
- $i=0;
- $cnt=mysql_num_rows($rsPtr);
- while($i
- {
- $rs[]=mysql_db_name($rsPtr,$i);
- $i++;
- }
- return $rs;
- }
function dbhalt($errmsg){
Notion AI
Notion是一款集成了笔记、知识库、数据表格、看板、日历等多种能力于一体的应用程序,它既可供个人使用,也可以与他人进行跨平台协作。
下载
- $msg="数据库有问题!";
- $msg=$errmsg;
- echo"$msg";
- die();
- }
//删
- function delete($sql){
- $result=$this->execute($sql,$dbbase);
- $this->affected_rows=mysql_affected_rows($this->dbLink);
- $this->free_result($result);
- return $this->affected_rows;
- }
//增
- function insert($sql){
- $result=$this->execute($sql,$dbbase);
- $this->insert_id=mysql_insert_id($this->dbLink);
- $this->free_result($result);
- return $this->insert_id;
- }
//改
- function update($sql){
- $result=$this->execute($sql,$dbbase);
- $this->affected_rows=mysql_affected_rows($this->dbLink);
- $this->free_result($result);
- return $this->affected_rows;
- }
-
- //关闭连接
- function dbclose(){
- mysql_close($this->dbLink);
- }
- }// end class
- ?>
-
复制代码
调用示例:
-
-
- include "class_database.php";
$mydb=new DataBase("localhost","root","123456","test");
- $mydb->connect();
- $mydb->execute("set names GBK");
- $mydb->execute("select * from usrs");
- print_r($mydb->dbNames());
- ?>
-
复制代码
|
PHP速学教程(入门到精通)
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
下载
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
文心一言
文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。
讯飞写作
基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿
即梦AI
一站式AI创作平台,免费AI图片和视频生成。
ChatGPT
最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。