PHP Package MySQL Class
Copy the Code code as follows:
Class Mysql {
Private $host;
Private $user;
Private $pwd;
Private $dbName;
Private $charset;
private $conn = null;
Public Function __construct () {
$this->host = ' localhost ';
$this->user = ' root ';
$this->pwd = ' root ';
$this->dbname = ' Test ';
$this->connect ($this->host, $this->user, $this->pwd);
$this->switchdb ($this->dbname);
$this->setchar ($this->charset);
}
Responsible for linking
Private function Connect ($h, $u, $p) {
$conn = mysql_connect ($h, $u, $p);
$this->conn = $conn;
}
Responsible for switching the database
Public Function Switchdb ($db) {
$sql = ' use '. $db;
$this->query ($sql);
}
Responsible for setting the character set
Public Function SetChar ($char) {
$sql = ' Set names '. $char;
$this->query ($sql);
}
Responsible for sending SQL queries
Public Function Query ($sql) {
Return mysql_query ($sql, $this->conn);
}
Responsible for obtaining select results for multiple rows and columns
Public Function GetAll ($sql) {
$list = Array ();
$rs = $this->query ($sql);
if (! $rs) {
return false;
}
while ($row = Mysql_fetch_assoc ($rs)) {
$list [] = $row;
}
return $list;
}
Public Function GetRow ($sql) {
$rs = $this->query ($sql);
if (! $rs) {
return false;
}
Return Mysql_fetch_assoc ($RS);
}
Public Function GetOne ($sql) {
$rs = $this->query ($sql);
if (! $rs) {
return false;
}
Return Mysql_fetch_assoc ($RS);
return $row [0];
}
Public function Close () {
Mysql_close ($this->conn);
}
}
Echo '
';
$mysql = new MySQL ();
Print_r ($mysql);$sql = "INSERT into Stu values (4, ' Wangwu ', ' 99998 ')";
if ($mysql->query ($sql)) {
echo "Query Success";
}else {
echo "Failure";
}
echo "
";
$sql = "SELECT * from Stu";
$arr = $mysql->getall ($sql);
Print_r ($arr);
?>