Recent Posts

PHP: Lớp kết nối MySQL với PHP bằng câu lệnh đơn giản

-
PHP Code:
class mysql{
var $property = array(
    'hostname'    => 'localhost',
    'database'    => '',
    'username'    => '',
    'password'    => '',
);
function mysql($info){
foreach($this->property as $k    => $v){
$this->property[$k]=$info[$k];
}
}
function connect(){
$flag = @mysql_pconnect($this->property['hostname'],$this->property['username'],$this->property['password']);
if(!$flag)    die("can not connect to server. may be server bussy");
$flag= @mysql_select_db($this->property['database'],$flag);
if(!$flag) die("can not select database now");
}
}
Lưu vào trong 1 file ví dụ “global.php”;
Để dễ dàng khi kết nối bạn viết thêm 1 file init.php file này có chứ năng tạo các thông tin cấu hình cơ bản như kết nối mysql, lấy thông tin thành viên …
ví dụ
PHP Code:
<?php
$dbinfo = array(
'hostname'    => 'localhost',
'database'    => 'databasename',
'username'    => 'root',
'password'    => 'root',
);
include("includes/global.php");
$db = new mysql($dbinfo);
$db->connect();
?>
Như vậy file nào cần kết nối database bạn chỉ cần inlclude file init.php vào là ok;
PHP Code:
include("init.php");

Tag: PHP, MySQL, Apache, Database, Connection, dbinfo




Klik untuk melihat kode: :) =( :s :D :-D ^:D ^o^ 7:( :Q :p T_T @@, :-a :W *fck* x@