您的位置 首页 php

11个程序员最常犯的MySQL错误(PHP开发)

本篇文章给大家介绍11个PHP程序员最常犯的MySQL错误。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

11个程序员最常犯的MySQL错误(PHP开发)

对于大多数web应用来说,数据库都是一个十分基础性的部分。如果你在使用PHP,那么你很可能也在使用MySQL—LAMP系列中举足轻重的一份子。

对于很多新手们来说,使用PHP可以在短短几个小时之内轻松地写出具有特定功能的代码。但是,构建一个稳定可靠的数据库却需要花上一些时日和相关技能。下面列举了我曾经犯过的最严重的11个MySQL相关的错误(有些同样也反映在其他语言/数据库的使用上)。。。

1、使用MyISAM而不是InnoDB

MySQL有很多数据库引擎,但是你最可能碰到的就是MyISAM和InnoDB。

MySQL 默认使用的是MyISAM。但是,很多情况下这都是一个很糟糕的选择,除非你在创建一个非常简单抑或实验性的数据库。外键约束或者事务处理对于数据完整性 是非常重要的,但MyISAM都不支持这些。另外,当有一条记录在插入或者更新时,整个数据表都被锁定了,当使用量增加的时候这会产生非常差的运行效率。

结论很简单:使用InnoDB。

2、使用PHP的mysql函数

PHP自产生之日就提供了MySQL库函数(or near as makes no difference)。很多应用仍然在使用类似mysql_connect、mysql_query、mysql_fetch_assoc等的函数,尽管PHP手册上说:

如果你在使用MySQL v4.1.3或者更新版本,强烈推荐使用您使用mysqli扩展。

mysqli(MySQL的加强版扩展)有以下几个优点:

可选的面向对象接口

prepared表达式,这有利于阻止SQL注入攻击,还能提高性能

支持更多的表达式和事务处理

另外,如果你想支持多种数据库系统,你还可以考虑PDO。

3、没有处理用户输入

这或者可以这样说#1:永远不要相信用户的输入。用服务器端的PHP验证每个字符串,不要寄希望与JavaScript。最简单的SQL注入攻击会利用如下的代码:

$username = $_POST["name"]; $password = $_POST["password"]; $sql = "SELECT userid FROM usertable WHERE username='$username' AND password='$password';"; // run query…

只要在username字段输入“admin';–”,这样就会被黑到,相应的SQL语句如下:

SELECT userid FROM usertable WHERE username='admin';

狡猾的黑客可以以admin登录,他们不需要知道密码,因为密码段被注释掉了。

4、没有使用UTF-8

美国、英国和澳大利亚的我们很少考虑除英语之外的其他语言。我们很得意地完成了自己的“杰作”却发现它们并不能在其他地方正常运行。

UTF-8解决了很多国际化问题。虽然在PHP v6.0之前它还不能很好地被支持,但这并不影响你把MySQL字符集设为UTF-8。

5、相对于SQL,偏爱PHP

如果你接触MySQL不久,那么你会偏向于使用你已经掌握的语言来解决问题,这样会导致写出一些冗余、低效率的代码。比如,你不会使用MySQL自带的AVG()函数,却会先对记录集中的值求和然后用PHP循环来计算平均值。

此外,请注意PHP循环中的SQL查询。通常来说,执行一个查询比在结果中迭代更有效率。

所以,在分析数据的时候请利用数据库系统的优势,懂一些SQL的知识将大有裨益。

6、没有优化数据库查询

99%的PHP性能问题都是由数据库引起的,仅仅一个糟糕的SQL查询就能让你的web应用彻底瘫痪。MySQL的EXPLAIN statement、Query Profiler,还有很多其他的工具将会帮助你找出这些万恶的SELECT。

7、不能正确使用数据类型

MySQL提供了诸如numeric、string和date等的数据类型。如果你想存储一个时间,那么使用DATE或者DATETIME类型。如果这个时候用INTEGER或者STRING类型的话,那么将会使得SQL查询非常复杂,前提是你能使用INTEGER或者STRING来定义那个类型。

很多人倾向于擅自自定义一些数据的格式,比如,使用string来存储序列化的PHP对象。这样的话数据库管理起来可能会变得简单些,但会使得MySQL成为一个糟糕的数据存储而且之后很可能会引起故障。

8、在查询中使用*

永远不要使用*来返回一个数据表所有列的数据。这是懒惰:你应该提取你需要的数据。就算你需要所有字段,你的数据表也不可避免的会产生变化。

9、不使用索引或者过度使用索引

一般性原则是这样的:select语句中的任何一个where子句表示的字段都应该使用索引。

举 个例子,假设我们有一个user表,包括numeric ID(主键)和email address。登录的时候,MySQL必须以一个email为依据查找正确的ID。如果使用了索引的话(这里指email),那么MySQL就能够使用 更快的搜索算法来定位email,甚至可以说是即时实现。否则,MySQL就只能顺序地检查每一条记录直到找到正确的email address。

有的人会在每个字段上都添加索引,遗憾的是,执行了INSERT或者UPDATE之后这些索引都需要重新生成,这样就会影响性能。所以,只在需要的时候添加索引。

10、忘记备份!

虽然比较罕见,但是数据库还是有崩溃的危险。硬盘有可能损坏,服务器有可能崩溃,web主机提供商有可能会破产!丢失MySQL数据将会是灾难性的,所以请确保你已经使用了自动备份或者已经复制到位。

11、Bonus mistake-不考虑使用其他数据库

对于PHP开发人员来说,MySQL可能是使用最广泛的数据库系统,但并不是唯一的选择。PostgreSQL和Firebird是最强有力的竞争者:这个两者都是开源的,而且都没有被公司收购。微软提供了sql server Express,甲骨文提供了10g Express,这两者都是企业级数据库的免费版本。有时候,对于一个较小的web应用或者嵌入式应用,SQLite也不失为一个可行的替代方案。

推荐学习:PHP视频教程

以上就是11个程序员最常犯的MySQL错误(PHP开发)的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:11个程序员最常犯的MySQL错误(PHP开发)

文章地址:https://www.zhihuclub.com/1550.shtml

关于作者: 智云科技

热门文章

评论已关闭

39条评论

  1. If you encounter websites that you think are illegally selling medicine, please report unlawful sales , Of Gelatin, Of Chocolate, Etc

  2. With the passage of best over the counter erection pills at walmart time, the more obvious the foresight of taking the initiative to join the best over the counter erection pills at walmart court and offer loyalty.

  3. The treatment is effective but controversial because studies published in 2002 and 2003 have previously suggested there is also link with breast cancer

  4. In a sharp contrast, the Alk5 iko females n 14, just like their Alk5 f f controls, presented no evidence of gross pathology at d28 Fig

  5. amiodarone will increase the level or effect of neratinib by affecting hepatic intestinal enzyme CYP3A4 metabolism YAP TAZ mediated DLL4 suppression is likely to be involved in LPA4 LPA6 mediated sprouting angiogenesis, as DAPT again restored sprouting defects in LPA4 LPA6 siRNA treated HUVECs

  6. The BT catheters were placed via a transabdominal approach with robotic assistance from a Da Vinci robot after a successful initial experience with a nonrobotic laparoscopic approach Except insofar as any conventional media or agent is incompatible with the active ingredient, its use in the therapeutic compositions of the disclosure is contemplated

  7. HcG exerts its effect by triggering ovulation after induction of follicular growth and development with administration of gonadotropins

  8. Throughout the pandemic, many people diagnosed with breast cancer who were scheduled to start radiation therapy did so with no delays

  9. In addition to this, it may also help improve the condition of your joints We re getting better all the time and hopefully will get better still

  10. It is widely and mistakenly believed that mothers who consume large amounts of vitamin C during pregnancy are at risk of giving birth to an infant with a higher than normal requirement for the vitamin

  11. Transdifferentiation depletes SC numbers, potentially leading to significant changes in the cellular organization of hearing and balance organs and loss of trophic support for the HCs A diagnosis of LCIS made by surgical excision does not require further surgical intervention, and there is no indication to document margin status in a specimen that contains only LN

  12. 2008, 24 3559 3569 efavirenz will decrease the level or effect of dexamethasone by affecting hepatic intestinal enzyme CYP3A4 metabolism

  13. Maunsell E, Brisson J, DeschГЄnes L, et al viagra avanafil ervaring Mortgage rates increased in recent weeks after the U

  14. Western blotting was performed to detect p JAK2, JAK2, p STAT3, STAT3, and GAPDH protein levels

  15. nifedipine will decrease the level or effect of amikacin by P glycoprotein MDR1 efflux transporter

  16. Mice were anesthetized by intraperitoneal injection of a mixture of 3 anesthetic agents 0 Some rather small previous studies 103 444 cases involving different ethnic study populations that have explored the potential association between the SULT1A1 c

  17. SARMs Supply claims that this is done through the aromatase inhibitor, which is said to work like an anti aromatase

  18. Methods to manipulate MSTN levels in the fully developed organism include the use of inhibitory MSTN antibodies 25 and propeptides 26 in wild type WT mice, as well as Mstn gene excision induced in transgenic mice with a tamoxifen or doxycycline DOX inducible Cre recombinase mutation 20, 22

  19. On comparison of the results of biodistribution from both the formulation of drug in the present study it is observed that, till 12 hrs after administration the maximum uptake and retention of the drug from both the tablet and NPs was found in ovary tissue which is the non target organ of the present investigation Figure 2 d

  20. Several limitations of the present study should be mentioned After numerous consultations at MSK, Eve began two courses of radiation treatment

  21. Larger volumes are infused selectively in patients with lower volume status So, in after you see my sincerity, you will definitely not treat me badly

网站地图