php mysql database class_PHP tutorial
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;
}
}
}

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

AI Hentai Generator
Generate AI Hentai for free.

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



MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
