My DataBase Class_PHP Tutorial
我的 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]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











IntelArrowLakeisexpectedtobebasedonthesameprocessorarchitectureasLunarLake,meaningthatIntel'sbrandnewLionCoveperformancecoreswillbecombinedwiththeeconomicalSkymontefficiencycores.WhileLunarLakeisonlyavailableasava

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

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

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.

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())

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

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.

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
