


Analysis of PHP encapsulated PDO database operation class examples
This article mainly introduces the PDO database operation class encapsulated by PHP, and analyzes related operation skills of PDO-based database addition, deletion, modification, transaction, batch addition and other related operation techniques based on PDO based on specific examples. Friends in need can refer to the following
The details are as follows:
<?php class DatabaseHandler { /** * sql语句查询 */ public static function query_data ($dataName,$sql,$query=array()) { $result = array(); if (!empty($sql)) { $data = Bj_PdoDB::factory($dataName)->allPrepare($sql, $query); if (!$data->isEmpty()) { $result = $data->data; } } return $result; } /** * 查询数据 */ public static function select_data ($dataName,$tableName,$where,$param=array(),$desc='') { $result = array(); $query = array(); $query_str = '1=1'; $tmp = ''; $str_arr = ''; if (!empty($param)) { $str = ''; foreach ($param as $val) { $str .= '`'.$val.'`,'; } $str = substr($str,0,-1); } $str_arr = !empty($str) ? $str : '*'; if ( !empty($where) ) { foreach ($where as $key => $value) { $tmp .= ' AND '.$key.'=:'.$key.''; $query[':'.$key] = $value; } } $query_str .= $tmp; if (!empty($desc)) { $query_str .= $desc; } $sql = "SELECT {$str_arr} FROM `".$tableName."` WHERE {$query_str} "; $query_data = Bj_PdoDB::factory($dataName)->allPrepare($sql, $query); if ( !$query_data->isEmpty() ) { $result = $query_data->data; } return $result; } /** * 批量添加信息 */ public static function insert_data($dataName,$tableName,$add_param){ $flag = false; if (!empty($add_param)) { $fileds = array(); $str = ''; foreach($add_param as $k=>$val){ $str .= '('; $i = 0; foreach ($val as $key=>$vl) { $fileds[$i] = '`'.$key.'`'; $str .= "'".addslashes($vl)."',"; $i++; } $str = substr($str,0,-1); $str .= '),'; } $filed_str = implode(',',$fileds); $val_str = substr($str,0,-1); $sql = "INSERT INTO `".$tableName."` ({$filed_str}) VALUES {$val_str}"; $ret = Bj_PdoDB::factory($dataName)->insertPrepare($sql); if ( !$ret->isEmpty() ) { $insertId = $ret->data; if($insertId > 0){ return $insertId; } } } return $flag; } /** * 删除数据 */ public static function delete_data ($dataName,$tableName,$id) { $query = array(':id' => $id); $sql = "DELETE FROM ". $tableName. " WHERE `id`=:id"; $db_res = Bj_PdoDB::factory($dataName)->simplePrepare($sql,$query); return $db_res->data; } /** * 更新数据 */ public static function update_data ($dataName,$tableName,$id,$array) { $flag = false; if ( !empty($array) && !empty($id)) { $query = array(':id' => $id); $up_str = ''; foreach($array as $key=>$val) { $query[':'.$key] = addslashes($val); $up_str .= '`'.$key.'`=:'.$key.','; } $up_str = substr($up_str,0,-1); $sql = "UPDATE `".$tableName."` SET ".$up_str." WHERE `id`=:id"; $db_res = Bj_PdoDB::factory($dataName)->changePrepare($sql, $query); $flag = true; } return $flag; } /** * 事务 */ public static function transactionHandler($dataName,$type){ switch ($type) { case 'begin': return Bj_PdoDB::factory($dataName)->beginTransaction(); break; case 'commit': return Bj_PdoDb::factory($dataName)->commit(); break; case 'rollback': return Bj_PdoDb::factory($dataName)->rollback(); break; default : exit; } } /** * curl获取数据get */ public static function curl_data ($curl_url){ if (!empty($curl_url)) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $curl_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $output = curl_exec($ch); curl_close($ch); return $output; } return false; } /** * curl提交数据 */ public static function curl_data_post ($ch_url,$param) { $result = array(); if (!empty($ch_url) && !empty($param)) { $query_string = http_build_query($param); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ch_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string); curl_setopt($ch, CURLOPT_HTTPHEADER,array("application/x-www-form-urlencoded; charset=UTF-8")); $output = curl_exec($ch); curl_close($ch); $result = !empty($output) ? json_decode($output,true) : array(); } return $result; } }
Related recommendations:
PHP PDO encapsulates static class code Share
php pdo encapsulates class code (supports transactions)
##php pdo encapsulation class implements mysql data addition, deletion, checking and modification
The above is the detailed content of Analysis of PHP encapsulated PDO database operation class examples. For more information, please follow other related articles on the PHP Chinese website!

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.


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 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
