自己练练手,写一个一个功能比较简单的MySQL数据库操作类
<?php /** * Created by JetBrains PhpStorm. * User:JAE * Date: 13-8-13 * Time: 下午5:15 * Blog:http://blog.jaekj.com * QQ:734708094 * 通用数据库操作类 * 版本:V1.1 */ /* 数据库配置 return array( 'DB_CONFIG' => array( //数据库配置 'DB_HOST'=>'127.0.0.1', //服务器地址 'DB_NAME' => 'tmp', // 数据库名 'DB_USER' => 'root', // 用户名 'DB_PWD' => '', // 密码 'DB_ENCODE'=>'utf8',//编码 'DB_PREFIX' => 'dmtx_' // 数据库表前缀 ) ); */ class M { private $link; //数据库连接 private $table; //表名 private $prefix; //表前缀 private $db_config; //数据库配置 /** * 参数:表名 数据库配置数组 或 数据库配置文件路径 * @param $table * @param string $db_config_arr_path */ function __construct($table, $db_config_arr_path = 'config.php') { if (is_array($db_config_arr_path)) { $this->db_config = $db_config_arr_path; } else { $this->db_config = require($db_config_arr_path); } $this->conn(); $this->table = $this->prefix . $table; } /** * 连接数据库 */ private function conn() { $db_config = $this->db_config; $host = $db_config["DB_CONFIG"]["DB_HOST"]; $user = $db_config["DB_CONFIG"]["DB_USER"]; $pwd = $db_config["DB_CONFIG"]["DB_PWD"]; $db_name = $db_config["DB_CONFIG"]["DB_NAME"]; $db_encode = $db_config["DB_CONFIG"]["DB_ENCODE"]; $this->prefix = $db_config["DB_CONFIG"]["DB_PREFIX"]; $this->link = mysql_connect($host, $user, $pwd) or die('数据库服务器连接错误:' . mysql_error()); mysql_select_db($db_name) or die('数据库连接错误:' . mysql_error()); mysql_query("set names '$db_encode'"); } /** * 数据查询 * 参数:sql条件 查询字段 使用的sql函数名 * @param string $where * @param string $field * @param string $fun * @return array * 返回值:结果集 或 结果(出错返回空字符串) */ public function select($where = '1', $field = "*", $fun = '') { $rarr = array(); if (empty($fun)) { $sqlStr = "select $field from $this->table where $where"; $rt = mysql_query($sqlStr, $this->link); while ($rt && $arr = mysql_fetch_assoc($rt)) { array_push($rarr, $arr); } } else { $sqlStr = "select $fun($field) as rt from $this->table where $where"; $rt = mysql_query($sqlStr, $this->link); if ($rt) { $arr = mysql_fetch_assoc($rt); $rarr = $arr['rt']; } else { $rarr = ''; } } return $rarr; } /** * 数据更新 * 参数:sql条件 要更新的数据(字符串 或 关联数组) * @param $where * @param $data * @return bool * 返回值:语句执行成功或失败,执行成功并不意味着对数据库做出了影响 */ public function update($where, $data) { $ddata = ''; if (is_array($data)) { while (list($k, $v) = each($data)) { if (empty($ddata)) { $ddata = "$k='$v'"; } else { $ddata .= ",$k='$v'"; } } } else { $ddata = $data; } $sqlStr = "update $this->table set $ddata where $where"; return mysql_query($sqlStr); } /** * 数据添加 * 参数:数据(数组 或 关联数组 或 字符串) * @param $data * @return int * 返回值:插入的数据的ID 或者 0 */ public function insert($data) { $field = ''; $idata = ''; if (is_array($data) && array_keys($data) != range(0, count($data) - 1)) { //关联数组 while (list($k, $v) = each($data)) { if (empty($field)) { $field = "$k"; $idata = "'$v'"; } else { $field .= ",$k"; $idata .= ",'$v'"; } } $sqlStr = "insert into $this->table($field) values ($idata)"; } else { //非关联数组 或字符串 if (is_array($data)) { while (list($k, $v) = each($data)) { if (empty($idata)) { $idata = "'$v'"; } else { $idata .= ",'$v'"; } } } else { //为字符串 $idata = $data; } $sqlStr = "insert into $this->table values ($idata)"; } if(mysql_query($sqlStr,$this->link)) { return mysql_insert_id($this->link); } return 0; } /** * 数据删除 * 参数:sql条件 * @param $where * @return bool */ public function delete($where) { $sqlStr = "delete from $this->table where $where"; return mysql_query($sqlStr); } /** * 关闭MySQL连接 * @return bool */ public function close() { return mysql_close($this->link); } } //$hj = new M("user"); //echo $hj->insert("NULL,'hj.q@qq.com','cde'"); //$arr = $hj->select(); //print_r($arr); //echo $hj->update("id>3",array("email"=>"bn@c.cc"));
![[php]自己写的一个MySQL数据库操作类_PHP教程](http://www.bkjia.com/uploadfile/2013/0910/20130910090748157.jpg)
<?php require 'Query/M.class.php'; $hj = new M("user"); //增 echo $hj->insert(array("NULL","1@qq.com","xxx")); echo $hj->insert(array("email"=>"12@qq.com","password"=>"cccc")); echo $hj->insert("NULL,'123@qq.com','cde'"); //查 //查看所有数据 $arr = $hj->select(); print_r($arr); //查看id大于3且id小于6 的id 和email 字段 的所有数据 $arr = $hj->select("id>3 and id<6","id,email"); print_r($arr); //获取数据库中一共有多少条记录 $rt = $hj->select("1","*","count"); echo $rt; // 改 $b = $hj->update("id=1","email = '1234@qq.com'"); echo $b; $b = $hj->update("id>3 and id<6",array("email"=>"1234@qq.com")); echo $b; // 删 $b = $hj->delete("id>3"); echo $b; //关闭 $hj->close();

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver Mac版
视觉化网页开发工具

Dreamweaver CS6
视觉化网页开发工具

SublimeText3汉化版
中文版,非常好用