dps69208 2014-12-14 18:22
浏览 24
已采纳

Php Mysqli问题和处理程序库/类

I have been programming php for quite some time now. But unfortunately I lack the profound understanding of few things, and I'm trying to make up for that.

I have asked a few specific questions like how to do this, how to do that, and always focused on achieving things with code rather than just learning for deeper understanding. so this is the first time I ask such an "open" question like this.

I'm planning to create an OOP system with PHP. I'm trying to put everything I have learned thus far into this system. Using the best practices and skills I have acquired up until this moment. The point I'm looking to learn more about is my system Mysqli DB handler.

Rather than just ask questions I will also try answer them myself, so maybe you are kind enough to give me feedback on my way of thinking or confirm the information I have gathered from different resources before asking here.

My first question is why do I need a DB handler class?

So far, I have been calling normal (raw) mysqli statments in my Models (classes) without the need of a handler. But I have seen many systems using a DB handler class. My conclusion is, it saves time. You can write and manipulate MySQL statements quicker than writing them as raw. Is this the only advantage? Another advantage I might think of is safety. I can validate the parameters in my Handler class, and since the validation happens in one place it's more safe, than having to validate parameters in multiple places, increasing the chance of forgetting to validate a parameter or such...

My Second question is, What is exactly preparing a mysql statement, and how does it eliminate the risk of mysql injection? (What is special about it's working that it's different that writing the parameter right into the raw query?)

My Third question is, is PDO required to parameterize queries? A friend of mine told me I must install PDO driver if I want to prepare statements properly. But after researching some, I saw that mysqli has the same ability to prepare and bind parameters to the statements. Is there credibility to his claim?

My 4th question is, according to your experience, can you suggest a good Mysqli DB handler library that is open to be shared and used in my project, also that I can learn from?

I know I have asked many questions, and I REALLY appreciate your time and reply in advance!

Thank you very much.

ShadyAF

  • 写回答

1条回答 默认 最新

  • dongqiao5573 2014-12-14 19:36
    关注
    1. You don't need a DB class handler, you can implement if you want but it is not a need.
    2. What is exactly preparing a mysql statement ?
      SQL Syntax for Prepared Statements
      Prepared Statements
      How can I prevent SQL-injection in PHP?

    3. PDO is not the only extensions that offers prepared statment. You can do it with mysqli see mysqli_prepare. This claim is wrong.

    4. According to my experience, PDO is better than mysqli but either way I would use them raw because I came to the conclusion that such classes just limit you. If you want to use one you can always write your own (not recommended), google or ask on software-recs.
    5. (Bonus :D)I think what you are really looking for is a framework. I would suggest Zend or Laravel.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料