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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测