Class kết nối MySql
Đại Học Văn Hiến Liên Thông :: TÀI LIÊU,THẮC MẮC,GÓP Y :: TRAO ĐỔI VỀ LUẬN VĂN :: THIẾT KẾ - LẬP TRÌNH WEB
Trang 1 trong tổng số 1 trang
Class kết nối MySql
Tạo 1 file có tên là connect.php. Thông tin về hàm có trong ghi chú
Example:
Tạo 1 file có tên là index.php
- Code:
<?php
/**
* Created by NGUYEN HONG KHANH.
* Date: 12/20/12
* Time: 5:18 PM
* Email: nhkitct11121988@gmail.com or hacker_alone1112@yahoo.com
*
* class name:mysql
* description:database abstraction class
* purpose:to connect to mysql database and simplify sql queries
* methods used:database(constructor),connect(),query(),num_rows(),movenext(),getfield, getinsertID
*
**/
class Database {
//stores hostname or the server name
var $conn;
//stores the resource ID for the sql query
var $rs;
//stores properties of the record corresponding to that column
var $record;
//returns number of records returned by the sql query
var $num;
//returns the id of the latest inserted record
var $latestID;
/****************************
function name:mysql(constructor)
description:constructor
purpose:to set values for the variables, calls up the connect method()
arguments:hostName, dbName, $userName, $password
returns:nothing
***********************************/
function Database() {
$hostName = ""; /*Ex: localhost or 127.0.0.1*/
$userName = ""; /*MySql Username. Ex: root*/
$password = ""; /*MySql Password. Ex: root*/
$dbName = ""; /*Database name. Ex: test*/
$this->hostName = $hostName;
$this->userName = $userName;
$this->password = $password;
$this->dbName = $dbName;
/*Call function connect*/
$this->connect();
}
/*****************************
function name:connect()
description:open a connection to mysql
purpose:to open a connection to mysql database and select the database
arguments:nothing
returns:connection ID
******************************/
function connect() {
$this->conn = @mysql_pconnect($this->hostName, $this->userName, $this->password)
or die("Connection to the server failed");
//select the database
@mysql_select_db($this->dbName)
or die("No such database exist");
@mysql_query("SET NAMES utf8", $this->conn);
return $this->conn;
}
/****************************
function name:query()
description:pass sql query to the database
purpose:to execute sql query
arguments:$sql(sql query)
returns:resource ID
*****************************/
function query($sql) {
$this->rs = @mysql_query($sql, $this->conn)
or die("There is error in the sql query");
return $this->rs;
}
/****************************
function name:num_rows
description:gives numbers of rows
purpose:gives number of rows returned by sql query
arguments:nothing
returns:number of rows
*****************************/
function num_rows() {
$num = @mysql_num_rows($this->rs);
return $num;
}
/*****************************
function name:movenext()
description:navigate the database
purpose:to fetch an object with properties that correspond to the fetched row
arguments:nothing
returns:returns an object with properties of the fetched row
*****************************/
function moveNext() {
$this->record = @mysql_fetch_object($this->rs);
return $this->record;
}
/****************************
function name:getfield()
description:retrieves the field value
purpose:to get the field value corresponding to that record
arguments:nothing
returns:field value corresponding to that record
****************************/
function getfield($field) {
return $this->record->$field;
}
/****************************
function name:getinsertID()
description:retrieves the latest ID
purpose:to get the ID of the latest inserted record
(works only with auto increment fields of the database)
arguments:nothing
returns:ID of the latest inserted record
*****************************/
function getinsertID() {
$this->latestID = @mysql_insert_id($this->conn);
return $this->latestID;
}
/**********************
function name:close()
description:close the connection
purpose:to close connection with mysql database
arguments:nothing
returns:true or false (boolean value)
***********************/
function close() {
return @mysql_close($this->conn);
}
}
Example:
Tạo 1 file có tên là index.php
- Code:
<?php
require("connect.php");
$dbConn = new database();
$dbConn->query("select * from table");
if($dbConn->num_rows()>0){
while($dbConn->moveNext()){
echo $dbConn->getField("firstName");
echo "<br>";
}
}
?>
admin- Quản trị diễn đàn
- Tổng số bài gửi : 202
Join date : 01/05/2012
Age : 35
Đại Học Văn Hiến Liên Thông :: TÀI LIÊU,THẮC MẮC,GÓP Y :: TRAO ĐỔI VỀ LUẬN VĂN :: THIẾT KẾ - LẬP TRÌNH WEB
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|