


PHP implementation can be used for mysql, mssql, pg database operation class, mysqlmssql_PHP tutorial
php implementation can be used for mysql, mssql, pg database operation class, mysqlmssql
The examples in this article describe the database operation classes that can be used in three databases: mysql, mssql, and pg. You can easily change the type of your database by making any modifications. Share it with everyone for your reference. The specific analysis is as follows:
Function list, index:
Open: Open database connection Line:71
Close: Close the database connection Line:107
SelectDB: Select database Line:129
Query:Create query Line:151
DataSeek: Move record pointer Line:175
FieldName: Get the field name Line:198
FieldType: Get the field type Line:220
FieldLenght: Get the field length Line:242
FetchRow: Get data and save to array (numeric index) Line:264
FetchArray: Get data and save it into an array (number and association) Line:289
FetchObject: Get data and save it to the object (object mode) Line:315
Result: Get the result data Line:341
FreeResult: Refresh the record set Line:363
RowsNumber: Get the number of records Line:385
FieldsNumber: Get the number of fields Line:407
CurRecNumber: Get the current record number (starting from 0) Line:429
RecordNumber: Get the current line number (starting from 1) Line:438
MoveFirstRec: Move to the first record Line:447
MoveLastRec: Move to the last record Line:469
MovePreviousRec: Move to the previous record Line:495
MoveNextRec: Move to the next record Line:521
MoveToRec: Move to a specific record (starting from 1) Line:548
php database operation code is as follows:
This class encapsulates database operations and has good portability. For databases: mysql, mssql, pg
*************************************************** **********************************
// - function list index:
// - Open: Open the database connection Line:71
// - Close: Close the database connection Line:107
// - SelectDB: Select database Line:129
// - Query: Create query Line:151
// - DataSeek: Move record pointer Line:175
// - FieldName: Get the field name Line:198
// - FieldType: Get the field type Line:220
// - FieldLenght: Get the field length Line:242
// - FetchRow: Get data and save to array (numeric index) Line:264
// - FetchArray: Get data and save it into an array (numeric and associative) Line:289
// - FetchObject: Get data and save it to the object (object mode) Line:315
// - Result: Get the result data Line:341
// - FreeResult: Refresh the record set Line:363
// - RowsNumber: Get the number of records Line:385
// - FieldsNumber: Get the number of fields Line:407
// - CurRecNumber: Get the current record number (starting from 0) Line:429
// - RecordNumber: Get the current line number (starting from 1) Line:438
// - MoveFirstRec: Move to the first record Line:447
// - MoveLastRec: Move to the last record Line:469
// - MovePreviousRec: Move to the previous record Line:495
// - MoveNextRec: Move to the next record Line:521
// - MoveToRec: Move to a specific record (starting from 1) Line:548
*************************************************** **********************************
//Inputs:
// - dbType: databases type: mssql, mysql, pg
// - connectType: connection type: c - common connection,
// p - open persistent connection
// - connect: for MS SQL Server - server name,
// for MySQL - hostname [:port] [:/path/to/socket] ,
// for PostgreSQL - host, port, tty, options,
// dbname (without username and password)
// - username
// - password
// - dbName: database name
// - query: SQL query
// - result: result set identifier
// - RowNumber:
// - offset: field identifier
// - ResultType: a constant and can take the following values: PGSQL_ASSOC, PGSQL_NUM, and PGSQL_BOTH
// - FieldName
//
//Returns:
// - result: result set identifier
// - connect link identifier
// - record number (starting at 0: CurrRecNumber or starting at 1: RecordNumber)
// - number of fields in the specified result set
// - number of rows in the specified result set
*************************************************** *************************************/
Class mDatabase
{
/***********************************Member variable definition****************** *************************/
var $dbType; // 数据库类型: mssql, mysql, pg
var $connectType; // 连接类型: c - common connection, p - open persistent connection
var $idCon; // 连接号
var $curRow; // current row number of data from the result
// associated with the specified result identifier array
var $seek; // current row number of data from DataSeek function array
/***********************************Member method implementation****************** *************************/
/***************************************************** *********************************
*Function to connect to database
*************************************************** *************************************/
Function Open($dbType, $c, $connect, $username = "", $password = "")
{
$this->dbType = $dbType;
Switch ($dbType) {
Case "mssql":
If ($connectType == "c") {
$idCon = mssql_connect($connect, $username, $password);
} Else {
$idCon = mssql_pconnect($connect, $username, $password);
}
Break;
Case "mysql":
If ($connectType == "c") {
$idCon = mysql_connect($connect, $username, $password);
} Else {
$idCon = mysql_pconnect($connect, $username, $password);
}
Break;
Case "pg":
If ($connectType == "c") {
$idCon = pg_connect($connect . " user=" . $username . " password=" . $password);
} Else {
$idCon = pg_pconnect($connect . " user=" . $username . " password=" . $password);
}
Break;
Default:
$idCon = 0;
Break;
}
$this->idCon = $idCon;
Return $idCon;
}
/***************************************************** *********************************
*Close database connection
*************************************************** *************************************/
Function Close()
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_close($this->idCon);
Break;
Case "mysql":
$r = mysql_close($this->idCon);
Break;
Case "pg":
$r = pg_close($this->idCon);
Break;
Default:
$r = False;
Break;
}
Return $r;
}
/***************************************************** *********************************
*Select database
*************************************************** *************************************/
Function SelectDb($dbName)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_select_db($dbName);
Break;
Case "mysql":
$r = mysql_select_db($dbName);
Break;
Case "pg":
$r = False;
Break;
Default:
$r = False;
Break;
}
Return $r;
}
/***************************************************** *********************************
*Create query
*************************************************** *************************************/
Function Query($query)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_query($query, $this->idCon);
Break;
Case "mysql":
$r = mysql_query($query, $this->idCon);
Break;
Case "pg":
$r = pg_exec($this->idCon, $query);
Break;
Default:
$r = False;
Break;
}
$this->curRow[$r] = 0;
$this->seek[$r] = 0;
Return $r;
}
/***************************************************** *********************************
*Move record pointer
*************************************************** *************************************/
Function DataSeek($result, $RowNumber)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_data_seek($result, $RowNumber);
Break;
Case "mysql":
$r = mysql_data_seek($result, $RowNumber);
Break;
Case "pg":
$r = False;
Break;
Default:
$r = False;
Break;
}
$this->seek[$result] = (int) $RowNumber;
Return $r;
}
/***************************************************** *********************************
*Get field name
*************************************************** *************************************/
Function FieldName($result, $offset)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_field_name($result, $offset);
Break;
Case "mysql":
$r = mysql_field_name($result, $offset);
Break;
Case "pg":
$r = pg_fieldname($result, $offset);
Break;
Default:
$r = False;
Break;
}
Return $r;
}
/***************************************************** *********************************
*Get field type
*************************************************** *************************************/
Function FieldType($result, $offset)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_field_type($result, $offset);
Break;
Case "mysql":
$r = mysql_field_type($result, $offset);
Break;
Case "pg":
$r = pg_fieldtype($result, $offset);
Break;
Default:
$r = False;
Break;
}
Return $r;
}
/***************************************************** *********************************
*Get field length
*************************************************** *************************************/
Function FieldLength($result, $offset)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_field_length($result, $offset);
Break;
Case "mysql":
$r = mysql_field_len($result, $offset);
Break;
Case "pg":
$r = pg_fieldsize($result, $offset);
Break;
Default:
$r = False;
Break;
}
Return $r;
}
/***************************************************** *********************************
*Get data and save it to an array, which can be accessed using numerical index
*************************************************** *************************************/
Function FetchRow($result, $RowNumber = 0)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_fetch_row($result);
Break;
Case "mysql":
$r = mysql_fetch_row($result);
Break;
Case "pg":
$r = pg_fetch_row($result, $RowNumber);
If ($r) {
$this->curRow[$result] = $RowNumber;
$this->seek[$result] = $RowNumber;
}
Break;
Default:
$r = False;
Break;
}
Return $r;
}
/***************************************************** *********************************
*Get data and save it to an array, which can be accessed using numeric index and associative index
*************************************************** *************************************/
Function FetchArray($result, $RowNumber = 0, $ResultType = 2)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_fetch_array($result);
Break;
Case "mysql":
$r = mysql_fetch_array($result);
Break;
Case "pg":
$r = pg_fetch_array($result, $RowNumber, $ResultType);
If ($r) {
$this->curRow[$result] = $RowNumber;
$this->seek[$result] = $RowNumber;
}
Break;
Default:
$r = False;
Break;
}
Return $r;
}
/***************************************************** *********************************
*Get data and save to object
*************************************************** *************************************/
Function FetchObject($result, $RowNumber = 0, $ResultType = 2)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_fetch_object($result);
Break;
Case "mysql":
$r = mysql_fetch_object($result);
Break;
Case "pg":
$r = pg_fetch_object($result, $RowNumber, $ResultType);
If ($r) {
$this->curRow[$result] = $RowNumber;
$this->seek[$result] = $RowNumber;
}
Break;
Default:
$r = False;
Break;
}
Return $r;
}
/***************************************************** *********************************
*Get result data
*************************************************** *************************************/
Function Result($result, $RowNumber, $FieldName)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_result($result, $RowNumber, $FieldName);
Break;
Case "mysql":
$r = mysql_result($result, $RowNumber, $FieldName);
Break;
Case "pg":
$r = pg_result($result, $RowNumber, $FieldName);
Break;
Default:
$r = False;
Break;
}
Return $r;
}
/***************************************************** *********************************
*Release result data
*************************************************** *************************************/
Function FreeResult($result)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_free_result($result);
Break;
Case "mysql":
$r = mysql_free_result($result);
Break;
Case "pg":
$r = pg_freeresult($result);
Break;
Default:
$r = False;
Break;
}
Return $r;
}
/***************************************************** *********************************
*Get the number of records
*************************************************** *************************************/
Function RowsNumber($result)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_num_rows($result);
Break;
Case "mysql":
$r = mysql_num_rows($result);
Break;
Case "pg":
$r = pg_numrows($result);
Break;
Default:
$r = False;
Break;
}
Return $r;
}
/***************************************************** *********************************
*Get the number of fields
*************************************************** *************************************/
Function FieldsNumber($result)
{
Switch ($this->dbType) {
Case "mssql":
$r = mssql_num_fields($result);
Break;
Case "mysql":
$r = mysql_num_fields($result);
Break;
Case "pg":
$r = pg_numfields($result);
Break;
Default:
$r = False;
Break;
}
Return $r;
}
/***************************************************** *********************************
*Get the current record number (starting from 0)
*************************************************** *************************************/
Function CurRecNumber($result)
{
$r = $this->curRow[$result];
Return $r;
}
/***************************************************** *********************************
* Get the current line number (starting from 1)
*************************************************** *************************************/
Function RecordNumber($result)
{
$cr = $this->CurRecNumber($result) + 1;
Return $cr;
}
/************************************************************************************
*移动到第一条记录
*************************************************************************************/
Function MoveFirstRec($result)
{
Switch ($this->dbType) {
Case "pg":
$r = $this->FetchRow($result, 0);
Break;
Default:
$rn = $this->DataSeek($result, 0);
If ($rn) {
$r = $this->FetchRow($result);
If ($r) $this->curRow[$result] = $this->seek[$result];
} Else {
$r = False;
}
Break;
}
Return $r;
}
/***************************************************** *********************************
*Move to last record
*************************************************** *************************************/
Function MoveLastRec($result)
{
$rs = $this->RowsNumber($result);
If ($rs) {
$rs--;
Switch ($this->dbType) {
Case "pg":
$r = $this->FetchRow($result, $rs);
Break;
Default:
$rn = $this->DataSeek($result, $rs);
If ($rn) {
$r = $this->FetchRow($result);
If ($r) $this->curRow[$result] = $this->seek[$result];
} Else {
$r = False;
}
Break;
}
}
Return $r;
}
/***************************************************** *********************************
*Move to previous record
*************************************************** *************************************/
Function MovePreviousRec($result)
{
$rs = $this->CurRecNumber($result);
If ($rs) {
$rs--;
Switch ($this->dbType) {
Case "pg":
$r = $this->FetchRow($result, $rs);
Break;
Default:
$rn = $this->DataSeek($result, $rs);
If ($rn) {
$r = $this->FetchRow($result);
If ($r) $this->curRow[$result] = $this->seek[$result];
} Else {
$r = False;
}
Break;
}
}
Return $r;
}
/***************************************************** *********************************
*Move to next record
*************************************************** *************************************/
Function MoveNextRec($result)
{
$rs = $this->CurRecNumber($result);
$rn = $this->RowsNumber($result);
$rs++;
If ($rs != $rn) {
Switch ($this->dbType) {
Case "pg":
$r = $this->FetchRow($result, $rs);
Break;
Default:
$re = $this->FetchRow($result);
If ($re) {
$r = $re;
$this->curRow[$result]++;
$this->seek[$result] = $this->curRow[$result];
} Else {
$r = False;
}
Break;
}
}
Return $r;
}
/***************************************************** *********************************
*Move to the specified record (number starts from 0)
*************************************************** *************************************/
Function MoveToRec($result, $RowNumber)
{
$rn = $this->RowsNumber($result);
If ($RowNumber > 0 And $RowNumber $RowNumber--;
Switch ($this->dbType) {
Case "pg":
$r = $this->FetchRow($result, $RowNumber);
Break;
Default:
$rn = $this->DataSeek($result, $RowNumber);
If ($rn) {
$r = $this->FetchRow($result);
If ($r) $this->curRow[$result] = $this->seek[$result];
} Else {
$r = False;
}
Break;
}
}
Return $r;
}
}
//******************************The method is implemented****************** *************************//
?>
希望本文所述对大家的PHP数据库程序设计有所帮助。

Effective methods to prevent session fixed attacks include: 1. Regenerate the session ID after the user logs in; 2. Use a secure session ID generation algorithm; 3. Implement the session timeout mechanism; 4. Encrypt session data using HTTPS. These measures can ensure that the application is indestructible when facing session fixed attacks.

Implementing session-free authentication can be achieved by using JSONWebTokens (JWT), a token-based authentication system where all necessary information is stored in the token without server-side session storage. 1) Use JWT to generate and verify tokens, 2) Ensure that HTTPS is used to prevent tokens from being intercepted, 3) Securely store tokens on the client side, 4) Verify tokens on the server side to prevent tampering, 5) Implement token revocation mechanisms, such as using short-term access tokens and long-term refresh tokens.

The security risks of PHP sessions mainly include session hijacking, session fixation, session prediction and session poisoning. 1. Session hijacking can be prevented by using HTTPS and protecting cookies. 2. Session fixation can be avoided by regenerating the session ID before the user logs in. 3. Session prediction needs to ensure the randomness and unpredictability of session IDs. 4. Session poisoning can be prevented by verifying and filtering session data.

To destroy a PHP session, you need to start the session first, then clear the data and destroy the session file. 1. Use session_start() to start the session. 2. Use session_unset() to clear the session data. 3. Finally, use session_destroy() to destroy the session file to ensure data security and resource release.

How to change the default session saving path of PHP? It can be achieved through the following steps: use session_save_path('/var/www/sessions');session_start(); in PHP scripts to set the session saving path. Set session.save_path="/var/www/sessions" in the php.ini file to change the session saving path globally. Use Memcached or Redis to store session data, such as ini_set('session.save_handler','memcached'); ini_set(

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
