 |
รบกวนช่วยดู Function การเชื่อมต่อ My SQL ตัวนี้ทีครับ |
|
 |
|
|
 |
 |
|
class database {
var $host;
var $user;
var $pass;
var $database;
var $persistent=0;
var $last_query;
var $result;
var $connection_id;
var $num_queries=0;
var $start_time;
function configure($host, $user, $pass, $database, $persistent=0){
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
$this->database=$database;
$this->persistent=$persistent;
return 1; //Success.
}
function connect(){
if(!$this->host) { $this->host="localhost"; }
if(!$this->user) { $this->user="root"; }
if($this->persistent){
$this->connection_id=mysql_pconnect($this->host, $this->user, $this->pass) or $this->connection_error();
}else{
$this->connection_id=mysql_connect($this->host, $this->user, $this->pass, 1) or $this->connection_error();
}
mysql_select_db($this->database, $this->connection_id);
return $this->connection_id;
}
function disconnect(){
if($this->connection_id) { mysql_close($this->connection_id); $this->connection_id=0; return 1; }
else { return 0; }
}
function change_db($database){
mysql_select_db($database, $this->connection_id);
$this->database=$database;
}
function query($query){
$this->last_query=$query;
$this->num_queries++;
$this->result=mysql_query($this->last_query, $this->connection_id) or $this->query_error();
return $this->result;
}
function fetch_row($result=0){
if(!$result) { $result=$this->result; }
return mysql_fetch_assoc($result);
}
function num_rows($result=0){
if(!$result) { $result=$this->result; }
return mysql_num_rows($result);
}
function insert_id(){
return mysql_insert_id($this->connection_id);
}
function connection_error(){
die("<b>FATAL ERROR:</b> Could not connect to database on {$this->host} (".mysql_error().")");
}
function query_error(){
die("<b>QUERY ERROR:</b> ".mysql_error()."<br />
Query was {$this->last_query}");
}
function fetch_single($result=0){
if(!$result) { $result=$this->result; }
return mysql_result($result, 0, 0);
}
function affected_rows($conn = NULL){
return mysql_affected_rows($this->connection_id);
}
}
จาก Code ข้างบนเราจะแทรกให้มันเชื่อมต่อกับฐานข้อมูลแบบ Utf-8 อย่างไรครับ
ปกติจะใช้
mysql_query('SET CHARACTER SET utf8');
แต่ทีนี้มันเป็น Function แบบนี้เลย ทำให้ งงไปตามๆกันเลยครับ
ขอบคุณครับ 
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2009-10-24 23:55:11 |
By :
Se7enKeNz |
View :
1130 |
Reply :
7 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่เก่งเรื่อง คลาส ซะด้วย แต่ลองแบบนี้ดูนะครับ
class database {
var $host;
var $user;
var $pass;
var $database;
var $persistent=0;
var $last_query;
var $result;
var $connection_id;
var $num_queries=0;
var $start_time;
var $chrset='SET CHARACTER SET utf8';
function configure($host, $user, $pass, $database, $persistent=0){
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
$this->database=$database;
$this->persistent=$persistent;
return 1; //Success.
}
function connect(){
if(!$this->host) { $this->host="localhost"; }
if(!$this->user) { $this->user="root"; }
if($this->persistent){
$this->connection_id=mysql_pconnect($this->host, $this->user, $this->pass) or $this->connection_error();
}else{
$this->connection_id=mysql_connect($this->host, $this->user, $this->pass, 1) or $this->connection_error();
}
mysql_select_db($this->database, $this->connection_id);
return $this->connection_id;
}
function disconnect(){
if($this->connection_id) { mysql_close($this->connection_id); $this->connection_id=0; return 1; }
else { return 0; }
}
function change_db($database){
mysql_select_db($database, $this->connection_id);
$this->database=$database;
}
function query($query){
$this->last_query=$query;
$this->num_queries++;
$this->result=mysql_query($this->last_query, $this->connection_id) or $this->query_error();
return $this->result;
}
function fetch_row($result=0){
if(!$result) { $result=$this->result; }
return mysql_fetch_assoc($result);
}
function num_rows($result=0){
if(!$result) { $result=$this->result; }
return mysql_num_rows($result);
}
function insert_id(){
return mysql_insert_id($this->connection_id);
}
function connection_error(){
die("<b>FATAL ERROR:</b> Could not connect to database on {$this->host} (".mysql_error().")");
}
function query_error(){
die("<b>QUERY ERROR:</b> ".mysql_error()."<br />
Query was {$this->last_query}");
}
function fetch_single($result=0){
if(!$result) { $result=$this->result; }
return mysql_result($result, 0, 0);
}
function affected_rows($conn = NULL){
return mysql_affected_rows($this->connection_id);
}
function querychr($strchrset){
$this->chrset=$srtchrset;
$this->result=mysql_query($this->chrset, $this->connection_id) or $this->query_error();
return $this->result;
}
}
ผิดถูกประการใด ขออภัยด้วยนะครับ แฮ่ ๆ 
|
 |
 |
 |
 |
Date :
2009-10-25 00:06:33 |
By :
noomna19 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไม่ได้ระบุชื่อ Database หรือเปล่าครับ แฮะๆ 
|
 |
 |
 |
 |
Date :
2009-10-25 00:32:57 |
By :
noomna19 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนแรกที่ยังไม่ได้ใส่ Code ด้านบนสามารถเข้าได้ปกติครับ
พอใส่ Code ข้างบนปุ๊ป Error เลยครับ 
|
 |
 |
 |
 |
Date :
2009-10-25 00:47:15 |
By :
Se7enKeNz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ก็ใส่มันตรง ๆ เลยสิครับพี่น้อง
function connect(){
if(!$this->host) { $this->host="localhost"; }
if(!$this->user) { $this->user="root"; }
if($this->persistent){
$this->connection_id=mysql_pconnect($this->host, $this->user, $this->pass) or $this->connection_error();
}else{
$this->connection_id=mysql_connect($this->host, $this->user, $this->pass, 1) or $this->connection_error();
}
mysql_select_db($this->database, $this->connection_id);
mysql_query("SET NAMES UTF8");
mysql_query("SET character_set_results=UTF8");
return $this->connection_id;
}
|
 |
 |
 |
 |
Date :
2009-10-25 01:44:32 |
By :
deawx |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับ ขอบคุณมากครับ 
|
 |
 |
 |
 |
Date :
2009-10-25 03:59:25 |
By :
Se7enKeNz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
QUERY ERROR: No database selected
Query was SELECT * FROM dd_settings
ยังไม่เลือกดาต้าเบสเลย
ก่อนใช้ฟังก์ชัน connect() คุณได้ทำการเรียก ฟังก์ชัน configure() ????
ตัวอย่างวิธีใช้
include('directory class');
$db =new database ;
$db->configure("","","","database name"); หรือ $db->configure($host, $user, $pass,"database name");
$db->connect();
หรือถ้ายุ่งยากก็แก้ค่า default $database ให้เป็นดาต้าเบสที่เราเรียกเลยก็ได้
สมมติ
....
var $host;
var $user;
var $pass;
var $database="database name"; //ทำค่า default ตรงนี้เลย
var $persistent=0;
var $last_query;
....
พอเราก็แก้ตรงนี้ต่อไปเราก็ไม่ต้องเรียก configure();
|
 |
 |
 |
 |
Date :
2009-10-25 04:18:33 |
By :
xbeginner01 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|