Class-Ready Connection database instantiation to use __ database
Source: Internet
Author: User
<?php
Class db{
Private $host;
Private $user;
Private $pass;
Private $db _name;
Private $sql;
Private $tab _name;
Private $cols;
The above is the declaration of the attribute
Public function __construct ($host, $user, $pass, $db _name, $sql) {
The constructor invokes this method before instantiating the object, providing an opportunity to initialize the property
When an object of a class is instantiated, the argument is passed, meaning that the argument is passed to the constructor
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->db_name = $db _name;
$this->sql = $sql;
The above is the initialization of the attribute
$this->connect ()//Call method function
}
Public Function connect () {
If the connection database successfully returns an identity, this representation is used to disconnect the database later
Mysql_connect ($this->host, $this->user, $this->pass);
mysql_select_db ($this->db_name);
mysql_query ($this->sql);
}
Query All content
Public function selects ($tab _name) {
$this->tab_name = $tab _name;
$sql = "Select *from {$tab _name}";
$rows =mysql_query ($sql);
$arr =array ();
while ($row = Mysql_fetch_array ($rows)) {
$arr []= $row;
}
return $arr;
}
Get a record (get associative array via MYSQL_FETCH_ASSOC, Mysql_fetch_row get index Array)
Public Function SelectRow ($tab _name, $cols) {
$this->cols = $cols;
$sql = "Select *from {$tab _name}";
$rows =mysql_query ($sql);
$res = Mysql_fetch_row ($rows);
return $res;
}
}
?>
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.