<?php
Class sqlhelper{
public $conn; Connecting resource variables
Public $host; Host
Public $user; User name
Public $password; Password
Public $db; Database
function __construct ($host = "localhost", $user = "root", $password, $db) {
$this->host= $host;
$this->user= $user;
$this->password= $password;
$this->db= $db;
$this->conn=mysql_connect ($this->host, $this->user, $this->password);
if (! $this->conn) {
Die ("Data connection Failed". Mysql_error ());
}
mysql_select_db ($this->db, $this->conn) or Die ("Database selection is incorrect". Mysql_error ());
mysql_query ("Set names UTF8"); Set character sets
}
function Exec_dql ($sql) {//query instruction
$res =mysql_query ($sql);
if ($res && mysql_num_rows ($res) >0) {
return $res;
}else{
Die ("query statement operation failed". Mysql_error ());
}
}
function Exec_dml ($sql) {//dml/delete change instruction
$res =mysql_query ($sql);
if ($res && mysql_affected_rows () >0) {
echo "Data operation succeeded";
}else{
echo "Data operation failed". Mysql_error ();
}
}
function Close_connect () {
if ($this->conn) {
Mysql_close ($this->conn);
}
}
}
$sqlHelper =new sqlHelper ("localhost", "root", "123456", "msyql");
$sql = "Select title from Dede_archives where typeid=87";
$res = $sqlHelper->exec_dql ($sql);
if ($res) {
while ($row =mysql_fetch_assoc ($res)) {
echo $row [' title ']. <br/> ";
}
}
?>
This article is from the "Cao Ruidong" blog, make sure to keep this source http://977520990.blog.51cto.com/7054422/1599997
A MySQL database tool class that encapsulates itself