A MySQL database tool class that encapsulates itself

Source: Internet
Author: User


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;


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";


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.