Home>
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;
//Create a constructor Database name Host name User name 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 ();
}
//Connect to the database
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 () and mysql_fetch_array (, mysql_assoc) fetch a row from the result set as an associative array, or return false if not
while ($this->rows=mysql_fetch_array ($this->result)) {
//Assign array to resultarray []=assign the number of affected rows to the array
$this->resultarray []=$this->rows
}
return $this->resultarray;
}
}
//$result return value is bool type false is no data
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
  • Previous Method for dynamically inserting CSS into JavaScript
  • Next C ++ implementation to find the greatest common divisor and the least common multiple