The examples in this article describe the connection MySQL class and usage in PHP encapsulation. Share to everyone for your reference, specific as follows:
Class mysql{Private $db _name, private $db _host, private $db _user, private $db _pwd, private $conn, private $querysql, PRI
Vate $result;
Private $resultarray =array ();
Private $row; Create constructor database name hostname username password 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 (); }//Connection database Private function Db_connect () {$this->conn=mysql_connect ($this->db_host, $this->db_user, $this-
>DB_PWD) or die ("could not Connect to MYSQL Server"); Private Function Selectdb () {mysql_select_db ($this->db_name) or Die ("Unable to select DBName")} Private function que Ry () {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 () and mysql_fetch_array (, MYSQL_ASSOC) take one row from the result set as an associative array, return false while ($this->rows=mysql_fetch_array ($this-≫result) {//Assignment array Assignment resultarray[]= the number of rows affected to the array $this->resultarray[]= $this->rows} return $this->
Resultarray;
}//$result The return value is bool type false to no data private function Get_num () {returns $this->num=mysql_num_rows ($this->result);}
$m =new MySQL ("testuser", "localhost", "root", "root");
$arreresult = $m->get_result ("SELECT * from UserInfo");
I hope this article will help you with your PHP programming.