doxn43207 2012-08-17 09:02
浏览 167
已采纳

使用PHP支持多种数据库类型

I've been wanting to build multi-database apps for a while and I still can't find anything about how to go about it.

All I would like to know is the best way to handle SQLite and MySQL queries. Do I need to have an IF statement with two different queries for each query I want to run?

Or is there an easy way to convert a MySQL statement into SQLite?

  • 写回答

2条回答 默认 最新

  • doubu7134 2012-08-17 09:03
    关注

    Use PHP's PDO.

    All you need to do is change the connection string and you can connect to different databases. As long as your SQL isn't written using functions specific to a particular database, it will just work with these databases:

    CUBRID (PDO) — CUBRID Functions (PDO_CUBRID)
    MS SQL Server (PDO) — Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
    Firebird/Interbase (PDO) — Firebird/Interbase Functions (PDO_FIREBIRD)
    IBM (PDO) — IBM Functions (PDO_IBM)
    Informix (PDO) — Informix Functions (PDO_INFORMIX)
    MySQL (PDO) — MySQL Functions (PDO_MYSQL)
    MS SQL Server (PDO) — Microsoft SQL Server Functions (PDO_SQLSRV)
    Oracle (PDO) — Oracle Functions (PDO_OCI)
    ODBC and DB2 (PDO) — ODBC and DB2 Functions (PDO_ODBC)
    PostgreSQL (PDO) — PostgreSQL Functions (PDO_PGSQL)
    SQLite (PDO) — SQLite Functions (PDO_SQLITE)
    4D (PDO) — 4D Functions (PDO_4D)
    

    It supports all the functions you want as well as many built in goodies preventing injection attacks and a bunch more.

    Edit: Some nice links for further reading as suggested by eldbiz

    http://php.net/manual/en/book.pdo.php http://www.php.net/manual/en/ref.pdo-sqlite.php http://www.php.net/manual/en/ref.pdo-mysql.php

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

报告相同问题?

悬赏问题

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