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
Related Post:
Code C#: About Dialog<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Title="About WPF" SizeToContent="WidthAndHeight" Background="Red"> <StackPanel> <Label FontWeight="Bold" FontSize="20" Foreground="blue"> WPF Version </Label> <Label>2012</Label> <Label>Installe… Read More
Code C#: Code Examples sample source codesCenter form window using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form { private Button myButton; public Form1() { this.AutoSc… Read More
Code C#: Tổng 2 số nguyên a, b nhập từ bàn phím (Console)using System;namespace Tong2so{ class Program { static void Main(string[] args) { int a, b, kq; Console.WriteLine(" Nhập gia tri a: "); a = int.Parse(Console.ReadLine()); … Read More
Code C#: Nhập, xuất với mảng 1 chiều (Console)using System;static void Main(string[] args){ int[] a = new int[10]; //nhap gia tri cho mang 1 chieu for (int i = 0; i < 10; i++){ Console.Write("Nhap a[{0}]=", i); a[i] = int.Parse(Console.ReadLine()); } //xuat mang 1 chieu for (int i… Read More
Code C#: Code Examples sample source codesGet selected checkbox list itemsusing System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.CheckedListBox chkListPossibleValues; private System.Windows.Forms.ListBox lstSelected; priv… Read More
Code C#: Giải phương trình bậc nhất (Console)using System; namespace PhuongTrinhBacNhat{ class Program { static void Main(string[] args) { Console.Write("Nhap he so a :"); int a = int.Parse(Console.ReadLine()); Console.Write("Nhap h… Read More
Klik untuk melihat kode: :) =( :s :D :-D ^:D ^o^ 7:( :Q :p T_T @@, :-a :W *fck* x@