Home > Article > Backend Development > PHP connection mysql database class_PHP tutorial
php connection mysql database class This is a simple and practical PHP class to connect to the MySQL database. It only performs data query, returns an array set, obtains a new ID, obtains a record row and other simple MySQL database connection codes.
php tutorial connection mysql tutorial database tutorial class
This is a simple and practical PHP class to connect to the MySQL database. It only performs data query, returns an array set, obtains a new ID, obtains a record row and other simple MySQL database connection codes.
*/
class database {
var $dblink;
function connect($dbhost, $dbuser, $dbpw, $dbname = "") {
$this->dblink = mysql_connect($dbhost, $dbuser, $dbpw);
mysql_query("set names 'utf8'");
mysql_query("set character_set_client=utf8");
mysql_query("set character_set_results=utf8");
if($dbname) {
Mysql_select_db($dbname, $this->dblink);
}
}
function query($sql) {
$result = mysql_query($sql, $this->dblink);
return $result;
}
function fetch_array($result) {
Return mysql_fetch_array($result);
}
function insert_id() {
$id = mysql_insert_id();
Return $id;
}
function getrow($sql) {
$result = mysql_query($sql, $this->dblink);
Return mysql_fetch_assoc($result);
}
function getdetailrow($sql) {
$result = mysql_query($sql, $this->dblink);
Return mysql_fetch_array($result);
}
//www.bKjia.c0m
function close() {
mysql_close($this->dblink);
}
}
//Call method
$db = new database;
$db->connect($dbhost, $dbuser, $dbpw, $dbname);
unset($dbhost, $dbuser, $dbpw, $dbname);
?>