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 > บทความจากสมาชิก > เริ่มต้นสร้าง PHP Lib กันดีกว่า



 
Clound SSD Virtual Server

เริ่มต้นสร้าง PHP Lib กันดีกว่า

ติดต่อกับ Mysql
ส่วนนี้คงไม่ต้อง อธิบายมากมายนะครับ แค่เป็นส่วนที่ต่อยอดจากการใช้งานฟังก์ชั่น mysql ใน php เฉยๆ เขียนให้มันทำงานได้กระชับมากขึ้นเท่านั้นเอง


MySql Class

Code
MySql.php
<?php
/**
* Mysql class database server for PHP5+
* @package Databases
*/
final class MySql {
/**
* @access Public
* @var string
*/
public $strServerName;
/**
* @access Public
* @public string
*/
public $strUserName;
/**
* @access Public
* @public string
*/
public $strUserPassword;
/**
* @access Public
* @public string
*/
public $strDatabaseName;
/**
* @access Private
* @public resource
*/
public $resConn;
/**
* @access Public
* @public string
*/
public $strSql;
/**
* @access Public
* @public resource
*/
public $resResult;
/**
* @access Private
* @public string
*/
public $rs;
/**
* @access Public
* @public integer
*/
public $intNumRows;

public $intNumColumn;
/**
* @access Public
* @public array
*/
public $fetch;
/**
* @access Public
* @public integer
*/
public $intAffretedRows;
/**
* @access Public
* @public integer
*/
public $intState;

/**
* @
* @return MySql
*/
function __construct() {
$this->strServerName = "";
$this->strUserName = "";
$this->strUserPassword = "";
$this->strDatabaseName = "";
$this->resConn = null;
$this->strSql = "";
$this->resResult = null;
$this->rs = "";
$this->intNumRows = 0;
$this->fetch = Array ();
$this->intAffretedRows = 0;
$this->intState = 0;

}
/**
* @access Public
* @return integer
*/
function openConnection() {
if ($this->intState == 0) {
$this->resConn = @mysql_connect ( $this->strServerName, $this->strUserName, $this->strUserPassword );
if ($this->resConn) {
$this->intState = 1;
}
return $this->intState;
} else {
$this->intState = 0;
return $this->intState;
}
}

/**
* @access Public
* @return boolean
*/
public function selectDatabase() {
if ($this->intState == 1) {
if (mysql_select_db ( $this->strDatabaseName, $this->resConn ) == true) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* @access Public
* @return integer
*/
public function closeDB() {
if ($this->intState == 1) {
mysql_close ( $this->resConn );
$this->intState = 0;
return $this->intState;
} else {
$this->intState = 1;
return $this->intState;
}
}
/**
* @access Public
* @return integer
*/
public function numFields() {
$i_num_fields = mysql_num_fields ( $this->resResult );
return $i_num_fields;
}
/**
* @access Public
* @param string $strSql
* @return resource
*/
public function executeQuery($s_sql, $b_use_charset = false, $s_charset = 'UTF-8') {
if ($b_use_charset === true)
@mysql_query ( "SET NAMES $s_charset" );

$this->resResult = @mysql_query ( $s_sql, $this->resConn );
if ($this->resResult) {
if (self::countRecord () > 0) {
self::fetchArray ();
}
} else {
print "SQL TEXT : " . $s_sql;
}
}

public function executeUpdate($s_sql) {
$i_affected = null;
$this->resResult = @mysql_query ( $s_sql, $this->resConn );
if ($this->resResult == true) {
$i_affected = self::rowAffreated ();
} else {
print "SQL TEXT : " . $s_sql;
}
return $i_affected;
}

/**
* @access Private
* @return integer
*/
public function countRecord() {
@$i_num_rows = mysql_num_rows ( $this->resResult );
return $i_num_rows;
}

public function countField() {
@$i_num_column = mysql_num_fields ( $this->resResult );
return $i_num_column;
}
/**
* @access Public
* @param integer $indexRow
* @param integer $indexField
* @return string
*/
public function indexGrid($indexRow, $indexField) {
$s_field_value = mysql_result ( $this->resResult, $indexRow, $indexField );
return $s_field_value;
}
/**
* @access Public
* @return array
*/
public function fetchArray() {
$this->fetch = mysql_fetch_array ( $this->resResult );
return $this->fetch;
}
/**
* @access Public
* @param integer $intIndexColumn
* @return string
*/
public function fieldName($intIndexColumn) {
$s_field_name = mysql_field_name ( $this->resResult, $intIndexColumn );
return $s_field_name;
}
/**
* return effect of sql on commond insert update and delete
*
* @access public
* @return integer
*/
public function rowAffreated() {
$i_affreted_rows = mysql_affected_rows ( $this->resConn );
return $i_affreted_rows;
}
}
?>

ตัวอย่างการใช้งาน คลาส MySql
require_once 'MySql.php';
header ( 'Content-Type: text/xml; charset=utf-8' );

# create new object.
$objMySql = new MySql ( );

# set server name.
$objMySql->strServerName = STR_NAME_SERVER;

# set username.
$objMySql->strUserName = STR_USER_NAME;

# set password.
$objMySql->strUserPassword = STR_PASSWORD;

# set database name.
$objMySql->strDatabaseName = STR_DATABASE_NAME;

# open connection.
$objMySql->openConnection ();

# open database.
$objMySql->selectDatabase ();

# define sql commnad.
$s_sql = "select * from tbl_userlogin;";

# on case select use $objMySql->executeQuery();
# on case insert, delete ,update use $objMySql->executeUpdate();
$objMySql->executeQuery ( $s_sql );

# count record from data of query
$intCountJob = $objMySql->countRecord ();

# show data of query by $objMySql->indexGrid(index row,index column or string name of column);
# count column of query by $objMySql->countField ();
# show field name of column by $objMySql->fieldName ( index Column)
print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
print "<tbl_userlogin totalItems =\"$intCountJob\" itemsFound=\"$intCountJob\">\r\n";

# loop record by data from job.
for($intRecordLoop = 0; $intCountJob > $intRecordLoop; $intRecordLoop ++) {
print "<record user_id=\"" . trim ( $objMySql->indexGrid ( $intRecordLoop, 'user_id' ) ) . "\">\r\n";
$intCountColumns = $objMySql->countField ();

#loop column.
for($intColumnLoop = 0; $intCountColumns > $intColumnLoop; $intColumnLoop ++) {
print "<" . $objMySql->fieldName ( $intColumnLoop ) . ">" . trim ( $objMySql->indexGrid ( $intRecordLoop, $objMySql->fieldName ( $intColumnLoop ) ) ) . "</" . $objMySql->fieldName ( $intColumnLoop ) . ">\r\n";
}
print "</record>\r\n";
}
print "</tbl_userlogin>\r\n";

# close database connection.
$objMySql->closeDB ();

ผลลัพธ์จากตัวอย่าง การใช้งาน Mysql Class ข้างต้น
test_mysql_class







   
Share
Bookmark.   

  By : เอฟ
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2009-01-29
  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 01
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 อัตราราคา คลิกที่นี่