Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > db class ใช้งานกับ php และ mysql



 
Clound SSD Virtual Server

db class ใช้งานกับ php และ mysql

เป็นตัวอย่างการเขียน class เพื่อไว้ดึงข้อมูลจาก db

Code (PHP)
<?php

class CReturn {
	const FAIL = false;
	const SUCCESS = true;
	public static function error($error_msg){
		return array(self::FAIL, $error_msg);
	}
	public static function success($value){
		return array(self::SUCCESS, $value);
	}
}

class initDb {
	private static $host = "localhost";
	private static $user = "root";
	private static $pass = "mysql";
	private static $db = "db_test1";	
	
	public static function connect(){
		$db_conn = mysql_pconnect(self::$host, self::$user, self::$pass) or die(mysql_error());
		mysql_select_db(self::$db, $db_conn) or die(mysql_error($db_conn));
		mysql_query("SET NAMES UTF8;");
		return $db_conn;
	}
}

class DbAbstract {
	protected $db_conn;
	protected $table_name;
	public $_data;
	public function __construct($db_conn){
		$this->db_conn = $db_conn;
	}
	protected function _get($sql){
		$this->_data = null;		
		$result = mysql_query($sql, $this->db_conn);
		if(mysql_errno($this->db_conn) !== 0) 
			return CReturn::error("Mysql error [".mysql_errno($this->db_conn)."] : ".$sql."<br />".mysql_error($this->db_conn));
		while($res = mysql_fetch_assoc($result)) {
			$this->_data[] = $res;
		}
		return CReturn::success(mysql_num_rows($result));
	}
	protected function getTableName(){
		return $this->table_name;
	}
	public function getAll(){
		return $this->_get("SELECT * FROM {$this->getTableName()}");
	}
}

class DbTable1 extends DbAbstract {
	protected $table_name = "table1";
	public function getById($id){
		return $this->_get("SELECT *  FROM {$this->getTableName()} WHERE user_id = {$id}");
	}
}

print "<pre>";
$db_conn = initDb::connect();

// แบบ error
$oTb1 = new DbTable1($db_conn);
list($result, $value) = $oTb1->getById("id_is_not_string");
print "<b>Query result</b> = ".var_export($result,true)."<br />";
print "<b>Num rows/Error msg</b> = '".$value."'<br />";
print "<b>data</b> = ".var_export($oTb1->_data,true)."<br />";
print "<br />";

// แบบ ดึงข้อมูลไม่เจอหรือไม่มีใน db
$oTb1 = new DbTable1($db_conn);
list($result, $value) = $oTb1->getById(5);
print "<b>Query result</b> = ".var_export($result,true)."<br />";
print "<b>Num rows/Error msg</b> = ".var_export($value,true)."<br />";
print "<b>data</b> = ".var_export($oTb1->_data,true)."<br />";
print "<br />";

// แบบดึงข้อมูลได้ 1 record
$oTb1 = new DbTable1($db_conn);
list($result, $value) = $oTb1->getById(1);
print "<b>Query result</b> = ".var_export($result,true)."<br />";
print "<b>Num rows/Error msg</b> = ".var_export($value,true)."<br />";
print "<b>data</b> = ".var_export($oTb1->_data,true)."<br />";
print "<br />";

// แบบดึงข้อมูลได้หลาย records
list($result, $value) = $oTb1->getAll();
print "<b>Query result</b> = ".var_export($result,true)."<br />";
print "<b>Num rows/Error msg</b> = ".var_export($value,true)."<br />";
print "<b>data</b> = ".var_export($oTb1->_data,true)."<br />";
print "<br />";

?>


สำหรับ db มีแค่นี้คับ
CREATE TABLE `table1` (
`user_id` int(11) NOT NULL auto_increment,
`fname` varchar(255) collate utf8_unicode_ci NOT NULL,
`lname` varchar(255) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

INSERT INTO `table1` (`user_id`, `fname`, `lname`) VALUES
(1, 'นายกอ', 'ไก่'),
(2, 'นายขอ', 'ไข่');


Output

Query result = false
Num rows/Error msg = 'Mysql error [1054] : SELECT * FROM table1 WHERE user_id = id_is_not_string
Unknown column 'id_is_not_string' in 'where clause''
data = NULL

Query result = true
Num rows/Error msg = 0
data = NULL

Query result = true
Num rows/Error msg = 1
data = array (
0 =>
array (
'user_id' => '1',
'fname' => 'นายกอ',
'lname' => 'ไก่',
),
)

Query result = true
Num rows/Error msg = 2
data = array (
0 =>
array (
'user_id' => '1',
'fname' => 'นายกอ',
'lname' => 'ไก่',
),
1 =>
array (
'user_id' => '2',
'fname' => 'นายขอ',
'lname' => 'ไข่',
),
)







   
Share
Bookmark.   

  By : nut_t02
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2009-05-14
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่