PHP MySQL开发中的一些经验
以下这些tips,是我在实际工作中慢慢形成的,有些可能是不正确的,有些出于个人习惯,所以,千万不要把以下这些条当成什么标准,其中可能隐藏着天大的bug,代码可能正在病态的运行中,SO!请一定仔细的看过后想想,这么做的好处是什么?会产生怎样的负面影响?
开发习惯和PHP代码
- 准确的理解各种概念。现在的新东西层出不穷,望文生义和一知半解对开发工作有害无益;
- 代码美观,适当的空行、缩进,空格,这样能更容易理解代码段的意思;
- 一定要写注释,而且要恰当的注释,要不然后面的维护工作或者接手代码的人会痛哭不已;
- 静态方法、类访问权限、接口、抽象类应该综合起来使用,发挥各自特点;
- 不要复制粘贴,即使是要用到现成的代码,也要一行一行的审阅后,再加入到新项目,因为经验告诉我们,这太容易出错了,对于使用开源类这种大段代码更需要;
- 变量都要初始化;
- 不要只处理error,而忽略warning和notice,这可能会导致日后的莫名其妙的问题,项目在开发状态下应该是error_reporting( E_ALL ^ E_NOTICE ),等到发布的外网生产环境时,应关闭所有错误报告display_errors=Off,error_reporting(0)网友 pAUL gAO分享了他们更合理的方案,error_reporting(E_ALL | E_STRICT),并且在生产环境中记录错误日志。
- 记录一些必要的错误日志,比如写文件失败、写memcache失败,socket连接失败、读写数据库失败,日志能够帮助出现问题时的快速定位,外部生产环境我个人是强烈建议关闭所有错误报告的;
- 用try、catch捕获异常,对代码的健壮有帮助,常常在API接口中碰到,这样子显得友好多了;
- 双引号中出现的变量建议加上大括号,至于是"${nider}at gmail.com"还是"{$tom}at zendstudio.net"看个人习惯,我更喜欢后面一种;
- 尽量少的if else嵌套层数,也许你要表达一个非常复杂的逻辑算法,但这样做至少能让代码逻辑更清晰
- 多阅读网上开源项目的优秀代码(不是优秀项目的开源代码),吸取其中值得借鉴的地方
- 语言包用sprintf的格式化来做是多么惬意的一件事啊!
- 写缓存并不总是要先serialize一次的
- AJAX传数据的时候,不要将数据库查出的数组直接json_encode后传给客户端,这样做不仅有一定的安全风险(字段名暴露),而且一些不需要的数据被传出浪费带宽,这条同样适用于API接口
- 要记得处理魔术变量,我的方法是直接关闭,当然也可以获取开关状态来避免传输数据被处理两次的问题
- 用$GLOBALS['var']代替global $var
- 不能轻易的die掉程序,尤其是在方法内部
- require、require_once、include、include_once有着略微不同的应用场景
- 为了最大限度的使得写入缓存成功,可以结合重试次数+usleep,我一般重试3次,还不行那就记下一条log了
- PHP的常量是个非常好的东西,很多开源项目中用一整个文件来定义要用到的常量
- 尽可能的使用绝对路径寻找文件
- autoload是个很灵活的东西
- 最好用上set_error_handler和set_exception_handler,那显得你的项目更完美
- PHP的引用类型是很高效的,在进行复杂运算时建议使用
- @符号抑制错误是很耗性能的,因此尽可能的找到替代方案
MySQL部分
- SQL语句用双引号,其中的值都用单引号,例如"INSERT INTO gril SET money='{$iMaxMoney}',age='18′"
- 用mysqli扩展代替mysql扩展
- 用mysqli_real_escape_string和mysqli_escape_string处理传出sql语句中的变量
- 用mysqli_set_charset(mysqli->set_charset)代替 query "SET NAMES"
- 联合查询(JOIN)之前,考虑下各个表的数据量,不合适的话应该分开查,尤其是有缓存可用的时候
- 很多地方需要记录发生时间,但不是每一个表都需要,同样,不是每一个表都需要一个自增量作主键
- 很多时候为integer类型加上unsigned是很好的
- INERT DELEYED、INSERT IGNORE、SELECT DISTINCT…这种语句通常有意想不到的好效果
- varchar类型并不是不能超过255长度,而是超过了255,这个字段就不能建立索引了,所以,看你的实际需要了
暂时就想到这么多,等再想到的继续update吧。想到什么写什么,没有什么条理性,多多包涵了,如果这些对你有点滴帮助,那我就感到非常开心了。

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 suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

Copying a table in MySQL requires creating new tables, inserting data, setting foreign keys, copying indexes, triggers, stored procedures, and functions. The specific steps include: creating a new table with the same structure. Insert data from the original table into a new table. Set the same foreign key constraint (if the original table has one). Create the same index. Create the same trigger (if the original table has one). Create the same stored procedure or function (if the original table is used).

Navicat for MariaDB cannot view the database password directly because the password is stored in encrypted form. To ensure the database security, there are three ways to reset your password: reset your password through Navicat and set a complex password. View the configuration file (not recommended, high risk). Use system command line tools (not recommended, you need to be proficient in command line tools).

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.
