douqiao5543 2014-01-11 19:48
浏览 80
已采纳

将PHP与数据库系统连接的PHP库如何实际调用数据库?

I've been looking through the Predis code on GitHub and it's massive so it's a bit difficult for me figure this out from just looking at the code because I don't understand what each folder/file is for

What I'd like to know is, how does PHP actually contact the database when using MySQL or Redis or any other database system? Does it make system calls similar to how you can do in C with system("some command here"); or does the developer have to actually extend the compiler for the PHP language to do this?

  • 写回答

2条回答 默认 最新

  • dongtuwu8548 2014-01-11 20:00
    关注

    You don't have to "extend the compiler", but you have to do what you probably meant by this: write a PHP module/extension in C that talks to the database's (typically) C library API. What does that mean? A database typically comes with connector libraries that are often written in low level C and thereby offer a C based API. That more or less works like an include 'api.php' which then allows you to call functions of whatever you just included, but it's specific to C. PHP code cannot talk to C code directly, but a PHP extension written in C can act as a "bridge" between PHP code and the C API.

    That C library then has many options how it may talk to the actual database. It may talk directly to another C API of the database, though that's not necessarily typical. Often a UNIX socket or TCP socket is used, sometimes across the network if the database is on a different machine. You could be talking directly to that UNIX/TCP socket from your PHP code if you wanted to, but that means you'd have to reimplement the entire protocol to talk to the database in PHP code. That's typically inefficient, since PHP is a rather high level language and doesn't offer any direct access to raw computer resources like memory, which makes this implementation rather inefficient.

    So, the way it typically goes is:

    • the database offers a protocol to talk to it over a socket of some kind
    • an official protocol client is implemented in a C library, because it's efficient and portable
    • someone writes a PHP extension to bridge that C library API into PHP userland code

    There's nothing stopping you from implementing that protocol in other languages in alternative clients, but since this is often a tedious process and C is a widely used system, people typically end up writing wrappers around the existing official C library.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度