search
HomeBackend DevelopmentPHP Tutorialphp mysql database class_PHP tutorial

php mysql database class_PHP tutorial

Jul 20, 2016 am 11:06 AM
mysqlphpDowndownloaddatabaseusekind

This is a commonly used mysql database class. Download it and take a look.

class mySqlClass {//Define the entire database access class mySqlClass
var $querynum = 0;//Query times variable

//Open the server connection--------- -------------------------------------------------- ------------------------------------------//
function connect( $dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0) {
//mysql_connect -- Open a connection to the MySQL server
//mysql_pconnect -- Open a connection to the MySQL server Persistent connection
//mysql_pconnect() and mysql_connect() are very similar, but there are two main differences.
//First, when connecting, this function will first try to find a (persistent) connection that has been opened on the same host with the same user name and password. If found, it will return this connection ID without opening a new connection. .
//Secondly, the connection to the SQL server will not be closed when the script is executed. This connection will remain open for future use (mysql_close() will not close the connection established by mysql_pconnect()).
//Determine whether the persistent connection setting is enabled. If it is enabled, the persistent connection will be used. If it is not enabled, the normal connection will be used.
if($pconnect) {
if(!@mysql_pconnect($dbhost, $dbuser, $dbpw )) {
$this->halt('1');
}
} else {
if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
$this->halt('1');
}
}
//Set the character encoding if the database version is greater than 4.1 and the character set parameter in the global variable is not empty------ -------------------------------------------------- -//
if($this->version() > '4.1') {
//$charset = 'gbk';
$charset = 'utf-8';
$dbcharset = '';
if(!$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
$dbcharset = str_replace( '-', '', $charset);
}
//mysql_query -- Send a MySQL query
//Set the NAMES parameter to the corresponding character set parameter value to avoid occurrences when PHP reads the mysql database Chinese garbled characters
if($dbcharset) {
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary");
}
//If the database version is greater than 5.0, set sql_mode The parameter is empty
if($this->version() > '5.0.1') {
mysql_query("SET sql_mode=''");
}
}
if($dbname) {//If the database name exists
//mysql_select_db -- Select MySQL database
//If the database selection is unsuccessful
mysql_select_db($dbname);
}
}

//mysql_select_db -- Select MySQL database------------------------------------------------ ----------------------------------//
function select_db($dbname) {
return mysql_select_db ($dbname);
}

//Read a data record function-------------------------- ------------------------------------------//
//mysql_fetch_array -- Get a row from the result set as an associative array, or a numeric array, or both
//The optional second parameter result_type in mysql_fetch_array() is a constant that can accept the following values: MYSQL_ASSOC, MYSQL_NUM and MYSQL_BOTH.
//MYSQL_ASSOC returns the data column using the field name as the index name of the array.
//MYSQL_BOTH The data column returned uses the field name and numeric index as the index name of the array.
//MYSQL_NUM The returned data column uses a numeric index as the index name of the array. The index starts at 0, indicating the first field of the returned result.
function fetch_array($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}

//Query database function ------- -------------------------------------------------- ------------//
function query($sql, $type = '') {
$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query' ) ?
'mysql_unbuffered_query' : 'mysql_query';
if(!($query = $func($sql)) && $type != 'SILENT') {
$this->halt( '0');
}
//Query times +1
$this->querynum++;
//Return query result resources
return $query;
}

//Return the number of affected records------------------------------------------------- ----------------------------//
//If the function is executed successfully, the number of recorded rows will be returned; if it fails, will return "-1".
function affected_rows() {
return mysql_affected_rows();
}

//Return the text error message generated by the previous MySQL operation------------ ----------------------------------//

function error() {
return mysql_error();
}

//Return the text error message integer type generated by the previous MySQL operation--------------------- ---------------//

function errno() {
return intval(mysql_errno());
}

/ /mysql_result -- Returns the field value in the result set---------------------------------------- -----//

function result($query, $row) {
$query = @mysql_result($query, $row);
return $query;
}

//mysql_num_rows -- Get the number of rows in the result set--------------------------------- ------------//

function num_rows($query) {
$query = mysql_num_rows($query);
return $query;
}

//mysql_num_fields -- Get the number of fields in the result set--------------------------------- ------------//

function num_fields($query) {
return mysql_num_fields($query);
}

//mysql_free_result -- Release the used result memory--------------------------------------------- ---//

function free_result($query) {
return mysql_free_result($query);
}

//mysql_insert_id -- Get the value generated by the previous INSERT operation ID------------------------------------------------//

function insert_id() {
$id = mysql_insert_id();
return $id;
}

//mysql_fetch_row -- Get a row from the result set as a numeric array ---------------------------------------------//

function fetch_row($query) {
$query = mysql_fetch_row($query);
return $query;
}

//mysql_field_name -- Get the field specified in the result Field name------------------------------------------------//

function field_name($query,$num=0){
return mysql_field_name($query,$num);
}

//mysql_fetch_field -- Get columns from the result set Information and returned as an object $num=0 indicates the first field---------------------------------//

function fetch_fields($query,$num=0) {
return mysql_fetch_field($query,$num);
}

//mysql_get_server_info -- Get MySQL server information--- ------------------------------------------//

function version() {
$MySqlVer = mysql_get_server_info();
if (empty($MySqlVer) || $MySqlVer==""){
$MySqlVer = $this->result(($ this->query("SELECT VERSION()")), 0);
}
if (empty($MySqlVer) || $MySqlVer==""){
$MySqlVer = '0 ';
}
return $MySqlVer;
}


//mysql_close -- Close the MySQL connection---------------- -------------------------------------------------- ---//

function close() {
return @mysql_close();
}


//Exception handling function halt ------ -------------------------------------------------- -------------//

function halt($mess) {
global $DataBaseBadFlag;
//$dberror = $this->error( );
//$dberrno = $this->errno(); //1114 personnel connection limit
if ($mess=="1"){
if ($DataBaseBadFlag!=1) {
echo("");
}
else{
if (!@function_exists ('phpEscape')){
require("escape.php");
}
echo phpEscape("Lock = false;location.href='pagedataerr.php';");
}
exit();
}
else{
return false;
}
}
}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445047.htmlTechArticleThis is a commonly used mysql database class. Download it and take a look. class mySqlClass {//Define the entire database access class mySqlClass var $querynum = 0;//Query times variable //Open...
Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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 Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools