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条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?