分类分类
更新时间:2026-09-29 21:32:53作者:fang
本文实例讲述了php封装的连接Mysql类及用法。分享给大家供大家参考,具体如下:
class mysql{
private $db_name;
private $db_host;
private $db_user;
private $db_pwd;
private $conn;
private $querysql;
private $result;
private $resultarray=array();
private $row;
//创建构造函数 数据库名 主机名 用户名 密码
function __counstruct($dbname,$dbhost,$dbuser,$dbpwd){
$this->db_name=$dbname;
$this->db_host=$dbhost;
$this->db_pwd=$dbpwd;
$this->db_user=$dbuser;
$this->dbconnect();
$this->selectdb();
}
//连接数据库
private function db_connect(){
$this->conn=mysql_connect($this->db_host,$this->db_user,$this->db_pwd) or die("Could not Connect MySql Server");
}
private function selectdb(){
mysql_select_db($this->db_name) or die("unable to select dbname")
}
private function query(){
return $this->result=mysql_query($this->querysql);
}
private function get_result($sql){
$this->querysql=$sql;
$this->query();
if($this->get_num()>0){
//mysql_fetch_assoc()和 mysql_fetch_array(,MYSQL_ASSOC)从结果集中取得一行作为关联数组 没有则返回false
while($this->rows=mysql_fetch_array($this->result)){
//赋值 数组赋值 resultarray[]= 将影响的行数赋值给数组
$this->resultarray[]=$this->rows
}
return $this->resultarray;
}
}
//$result返回值为 bool类型 false为没有数据
private function get_num(){
return $this->num=mysql_num_rows($this->result);
}
}
$m=new mysql("testuser","localhost","root","root");
$arreresult=$m->get_result("select * from userinfo");
希望本文所述对大家php程序设计有所帮助。
相关
超时空街球对决体育竞技1.35Gv1.72026-09-29
下载内蒙古农商手机银行金融理财162.14 MBv3.1.92026-09-29
下载爆射奇兵0.1折版角色扮演128.48 MBv1.0.安卓版2026-09-29
下载剑气除魔0.1折版策略游戏13.6 MBv1.0.02026-09-29
下载口袋喵喵策略游戏13.67 MBv1.0.02026-09-29
下载御龙城堡传奇手游策略游戏180.89 MBv1.0.22026-09-29
下载菲尼西雅战记策略游戏164.4 MBv1.2.12026-09-29
下载鏖战三国0.05折版角色扮演13.66 MBv1.0.02026-09-29
下载群英争雄策略游戏1007.72 Mv1.0.12026-09-29
下载梦漫app资讯阅读43.14 Mv1.0.12026-09-29
下载点点大作战经营养成623.91 Mv2.6.2002026-09-29
下载妖怪名单角色扮演13.64 Mv1.0.02026-09-29
下载







