Home Backend Development PHP Tutorial My DataBase Class_PHP Tutorial

My DataBase Class_PHP Tutorial

Jul 13, 2016 am 10:51 AM
class database database method kind solve Configuration




我的 DataBase类
解决方法




/**

* Database configuration class

*/



class DBConfig

{



public static $HOST = 'localhost';

public static $USERNAME = 'root';

public static $PASSWORD = 'root';

public static $DATABASE = 'shopping';

public static $CHARSET = 'utf8';



}



?>


复制代码



/**

* Database operation class

*/



class DataBase{



private $connection;



/**

*Construction method

* @access public

*/

public function __construct(){

$CONFIG = require(dirname(__FILE__).'/DBConfig.class.php');

$this>connection = mysql_connect(DBConfig::$HOST,DBConfig::$USERNAME,DBConfig::$PASSWORD);

mysql_select_db(DBConfig::$DATABASE);

mysql_query("SET NAMES '".DBConfig::$CHARSET."'");

}



/**

* Destruction method

* @access public

*/

public function __destruct(){

mysql_close($this>connection);

}



/**

* Execute SQL query statement

* @access private

* @param string $p_sql query command

* @return array record set, if no record returns an empty array

*/

private function query($p_sql){

$dataTemp = mysql_query($p_sql,$this>connection);

$data = array();

$dataItem = 0;

while ($rows = mysql_fetch_assoc($dataTemp)) {

$data[$dataItem] = $rows;

$dataItem++;

}

return $data;

}



/**

* Execute SQL statement

* @access public

* @param string $p_sql The SQL to be executed, which can be INSERT, SELECT, UPDATE or DELETE

* @return If SQL is SELECT, return the record set. If SQL is INSERT, return the new record ID. If SQL is UPDATE or DELETE, return the number of affected rows

*/

public function execute($p_sql){

$controlr = strtoupper(substr($p_sql,0,6));

switch ($controlr) {

case 'INSERT':

mysql_query($p_sql,$this>connection);

$result = mysql_insert_id($this>connection);

break;

case 'SELECT':

$result = $this>query($p_sql,$this>connection);

break;

default:

mysql_query($p_sql,$this>connection);

$result = mysql_affected_rows($this>connection);

break;

}

return $result;

}



}



?>


复制代码调用很简单:
$sql = '.....'; // 可以是任何增删改查的语句
$db = new DataBase();
$rs = $db>execute($sql);
$db = null;

请大家多多指教!

[ ]


D8888D回贴内容
看看呢 ~~ [img]http://www.phpchina.com/bbs/images/smilies/default/shy.gif[/img][img]http://www.phpchina.com/bbs/images/smilies/default/shy.gif[/img]

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632535.htmlTechArticle我的 DataBase类 解决方法 /*** Database configuration class*/ class DBConfig { public static $HOST = 'localhost'; public static $USERNAME = 'root'; public static $PASSWORD = 'root';...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Jun 15, 2024 pm 09:49 PM

IntelArrowLakeisexpectedtobebasedonthesameprocessorarchitectureasLunarLake,meaningthatIntel'sbrandnewLionCoveperformancecoreswillbecombinedwiththeeconomicalSkymontefficiencycores.WhileLunarLakeisonlyavailableasava

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors) How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors) May 07, 2024 pm 05:55 PM

Mobile phone film has become one of the indispensable accessories with the popularity of smartphones. To extend its service life, choose a suitable mobile phone film to protect the mobile phone screen. To help readers choose the most suitable mobile phone film for themselves, this article will introduce several key points and techniques for purchasing mobile phone film. Understand the materials and types of mobile phone films: PET film, TPU, etc. Mobile phone films are made of a variety of materials, including tempered glass. PET film is relatively soft, tempered glass film has good scratch resistance, and TPU has good shock-proof performance. It can be decided based on personal preference and needs when choosing. Consider the degree of screen protection. Different types of mobile phone films have different degrees of screen protection. PET film mainly plays an anti-scratch role, while tempered glass film has better drop resistance. You can choose to have better

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

Quickly learn how to unlock PUK (easily solve the problem of mobile phone lock screen) Quickly learn how to unlock PUK (easily solve the problem of mobile phone lock screen) Jun 01, 2024 pm 01:38 PM

We often encounter the embarrassing situation of forgetting the password or entering the wrong password and being unable to unlock the phone while using the phone. Let us regain the right to use our mobile phones, and PUK unlocking techniques can help us easily solve these problems. It also provides some practical tips to help readers better deal with mobile phone lock screen problems. This article will teach you the simplest method to unlock PUK and introduce the basic concepts and steps of PUK unlocking. Paragraph 1. What is PUK unlocking? The PUK code is a special code on the mobile phone card. PUK unlocking is a technology used to solve the problem of mobile phone lock screen and is used to unlock the PIN code of the SIM card. Understanding the basic concepts of PUK unlocking is the first step in learning this technique. 2. How to obtain the PUK code? The phone screen will display

See all articles