欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

简单的数据库链式操作的例子

程序员文章站 2022-03-26 17:30:39
...

/*

@author Dishy
数据库封装
*/
class mysqldb{

private$host;
private$username;
private$password;
private$dbname;
private$sql;
private$select = true;
private$result;
private$array;

publicfunction__construct($host,$username,$password,$dbname){
    $this->host = $host;
    $this->username = $username;
    $this->password = $password;
    $this->dbname = $dbname;
    $this->dbconnect();

}
/*
    数据库连接
*/privatefunctiondbconnect(){
    mysql_connect($this->host,$this->username,$this->password);
    mysql_select_db($this->dbname);

}
/*
*执行sql语句
*
**/publicfunctionquery($sql){
        $this->sql = $sql;
        $this->result = mysql_query($this->sql);
        return$this;
}
/*
*
*$select = false时输出sql语句
*/publicfunctionselect($select= true){
    $this->select = $select;
    if($this->select){
        return$this->array =  $this->result();
    }else{
        return$this->sql;
    }
}
/**
*结果集
*/privatefunctionresult(){
    $array = mysql_fetch_array($this->result);
    return$array;
}

}

?>
使用方法 include “./Api/datebase.php”;
define('DB_HOST_NAME','localhost');
define('DB_HOST_USERNAME','root');
define('DB_HOST_PASSWORD','root');
define('DB_HOST_DB','jciot');
$Jciot_db = new mysqldb(DB_HOST_NAME,DB_HOST_USERNAME,DB_HOST_PASSWORD,DB_HOST_DB);
$a = $Jciot_db->query('select * from jciot_member')->select();
var_dump($a);

?>`

以上就介绍了简单的数据库链式操作的例子,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。