Mysql.class.php

Source: Internet
Author: User
Tags php database

<?php
/**
*================================================================
* Mysql.class.php database Operation class, responsible for the database of the underlying operations
* @author Wang Suping
* @copyright Wisdom Podcast PHP Academy 2006-2013
* @version 1.0
* March 24, 2013 18:30:40
*================================================================
*/

Class mysql{
protected $conn = false; Database connection Resources
protected $sql; SQL statements

/**
* constructors, which are responsible for connecting servers, selecting databases, setting character sets, etc.
* @param $config String Configuration array
*/
Public function __construct ($config = Array ()) {
$host = Isset ($config [' Host '])? $config [' Host ']: ' localhost ';
$user = Isset ($config [' user '])? $config [' User ']: ' Root ';
$password = Isset ($config [' Password '])? $config [' Password ']: ';
$dbname = Isset ($config [' dbname '])? $config [' dbname ']: ';
$port = Isset ($config [' Port '])? $config [' Port ']: ' 3306 ';
$charset = Isset ($config [' CharSet '])? $config [' CharSet ']: ' 3306 ';

$this->conn = mysql_connect ("$host: $port", $user, $password) or Die (' Database connection error ');
mysql_select_db ($dbname) or Die (' Database selection error ');
$this->setchar ($charset);
}

/**
* Set Character Sets
* @access Private
* @param $charset String Character set
*/
Private Function SetChar ($charest) {
$sql = ' Set names '. $charest;
$this->query ($sql);
}

/**
* Execute SQL statement
* @access Public
* @param $sql string Query SQL statement
* @return $result, successful return of the resource, failure output error message, and exit
*/
Public Function Query ($sql) {
You can also add a switch to turn the SQL Log on/off
Save in an additional way
$temp = "[". Date (' y-m-d h:i:s '). "]" . $sql. Php_eol;
File_put_contents ("Log.txt", $temp, File_append);

$this->sql = $sql;
$result = mysql_query ($this->sql, $this->conn);

if (! $result) {
Die ($this->errno (). ': ' $this->error (). ' <BR/> Error statement for '. $this->sql. ' <br/> ');
}
return $result;
}

/**
* Get first field of first record
* @access Public
* SQL statement @param $sql string query
* @return Returns a value for this field
*/
Public Function GetOne ($sql) {
$result = $this->query ($sql);
$row = Mysql_fetch_row ($result);
if ($row) {
return $row [0];
} else {
return false;
}
}

/**
* Get a record
* @access Public
* @param SQL statement for $sql query
* @return Array Associative arrays
*/
Public Function GetRow ($sql) {
if ($result = $this->query ($sql)) {
$row = Mysql_fetch_assoc ($result);
return $row;
} else {
return false;
}
}

/**
* Get all the records
* @access Public
* @param SQL statements $sql executed
* @return $list A two-dimensional array of recorded elements
*/
Public Function GetAll ($sql) {
$result = $this->query ($sql);
$list = Array ();
while ($row = Mysql_fetch_assoc ($result)) {
$list [] = $row;
}
return $list;
}

/**
* Gets the value of a column
* @access Public
* @param SQL statements executed $sql string
* @return $list Array Returns a one-dimensional array consisting of the values of the column
*/
Public Function Getcol ($sql) {
$result = $this->query ($sql);
$list = Array ();
while ($row = Mysql_fetch_row ($result)) {
$list [] = $row [0];
}
return $list;
}


/**
* Gets the ID generated by the insert operation in the previous step
*/
Public Function Getinsertid () {
Return mysql_insert_id ($this->conn);
}
/**
* Get the error number
* @access Private
* @return Error number
*/
Public Function errno () {
Return Mysql_errno ($this->conn);
}

/**
* Get error messages
* @access Private
* @return Error private information
*/
Public Function error () {
Return mysql_error ($this->conn);
}

}

Mysql.class.php

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.