• php分享十五:php的数据库操作


    一:术语解释:

    What is an Extension?

    API和扩展不能理解为一个东西,因为扩展不一定暴露一个api给用户

    The PDO MySQL driver extension, for example, does not expose an API to the PHP programmer, but provides an interface to the PDO layer above it.

    The terms API and extension should not be taken to mean the same thing, as an extension may not necessarily expose an API to the programmer.

    参考:http://php.net/manual/zh/mysqlinfo.terminology.php

    二:扩展选择:


    ext/mysqli
    PDO_MySQLext/mysql
    PHP version introduced 5.0 5.1 2.0
    Included with PHP 5.x Yes Yes Yes
    Included with PHP 7.x Yes Yes No
    Development status Active Active Maintenance only in 5.x; removed in 7.x
    Lifecycle Active Active Deprecated in 5.x; removed in 7.x
    Recommended for new projects Yes Yes No
    OOP Interface Yes Yes No
    Procedural Interface Yes No Yes
    API supports non-blocking, asynchronous queries with mysqlnd Yes No No
    Persistent Connections Yes Yes Yes
    API supports Charsets Yes Yes Yes
    API supports server-side Prepared Statements Yes Yes No
    API supports client-side Prepared Statements No Yes No
    API supports Stored Procedures Yes Yes No
    API supports Multiple Statements Yes Most No
    API supports Transactions Yes Yes No
    Transactions can be controlled with SQL Yes Yes Yes
    Supports all MySQL 5.1+ functionality Yes Most No

    三:选择mysql驱动(Choosing a library)

    mysqli,mysql,pdo底层都是用c写的,有两种底层c库可以使用(mysqlnd library 和 libmysqlclient library),通过编译参数可以更改区别:

    mysqlnd更胜一筹;

    从php5.3开始,mysqlnd成为php的一部分发布,mysqlnd提供的高级功能有:lazy connections and query caching (这两个功能libmysqlclient library是没有的)

    编译配置:

    // Recommended, compiles with mysqlnd
    $ ./configure --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql=mysqlnd
    
    // Alternatively recommended, compiles with mysqlnd as of PHP 5.4
    $ ./configure --with-mysqli --with-pdo-mysql --with-mysql
    
    // Not recommended, compiles with libmysqlclient
    $ ./configure --with-mysqli=/path/to/mysql_config --with-pdo-mysql=/path/to/mysql_config --with-mysql=/path/to/mysql_config

    注意:可以分别指定mysqli, mysql, pdo 扩展的底层c库

     MySQL native driver (mysqlnd)MySQL client server library (libmysqlclient)
    Part of the PHP distribution Yes No
    PHP version introduced 5.3.0 N/A
    License PHP License 3.01 Dual-License
    Development status Active Active
    Lifecycle No end announced No end announced
    PHP 5.4 and above; compile default (for all MySQL extensions) Yes No
    PHP 5.3; compile default (for all MySQL extensions) No Yes
    Compression protocol support Yes (5.3.1+) Yes
    SSL support Yes (5.3.3+) Yes
    Named pipe support Yes (5.3.4+) Yes
    Non-blocking, asynchronous queries Yes No
    Performance statistics Yes No
    LOAD LOCAL INFILE respects the open_basedir directive Yes No
    Uses PHP's native memory management system (e.g., follows PHP memory limits) Yes No
    Return numeric column as double (COM_QUERY) Yes No
    Return numeric column as string (COM_QUERY) Yes Yes
    Plugin API Yes Limited
    Read/Write splitting for MySQL Replication Yes, with plugin No
    Load Balancing Yes, with plugin No
    Fail over Yes, with plugin No
    Lazy connections Yes, with plugin No
    Query caching Yes, with plugin No
    Transparent query manipulations (E.g., auto-EXPLAIN or monitoring) Yes, with plugin No
    
    
    
     MySQL native driver (mysqlnd)MySQL client server library (libmysqlclient)
    Part of the PHP distribution Yes No
    PHP version introduced 5.3.0 N/A
    License PHP License 3.01 Dual-License
    Development status Active Active
    Lifecycle No end announced No end announced
    PHP 5.4 and above; compile default (for all MySQL extensions) Yes No
    PHP 5.3; compile default (for all MySQL extensions) No Yes
    Compression protocol support Yes (5.3.1+) Yes
    SSL support Yes (5.3.3+) Yes
    Named pipe support Yes (5.3.4+) Yes
    Non-blocking, asynchronous queries Yes No
    Performance statistics Yes No
    LOAD LOCAL INFILE respects the open_basedir directive Yes No
    Uses PHP's native memory management system (e.g., follows PHP memory limits) Yes No
    Return numeric column as double (COM_QUERY) Yes No
    Return numeric column as string (COM_QUERY) Yes Yes
    Plugin API Yes Limited
    Read/Write splitting for MySQL Replication Yes, with plugin No
    Load Balancing Yes, with plugin No
    Fail over Yes, with plugin No
    Lazy connections Yes, with plugin No
    Query caching Yes, with plugin No
    Transparent query manipulations (E.g., auto-EXPLAIN or monitoring)

    四:mysql字符集设置

    字符设置必须用特定的函数来设置,而不能用query来处理

    <?php

    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");

    // Will NOT affect $mysqli->real_escape_string();
    $mysqli->query("SET NAMES utf8");

    // Will NOT affect $mysqli->real_escape_string();
    $mysqli->query("SET CHARACTER SET utf8");

    // But, this will affect $mysqli->real_escape_string();
    $mysqli->set_charset('utf8');

    // But, this will NOT affect it (utf-8 vs utf8) -- don't use dashes here
    $mysqli->set_charset('utf-8');

    ?>

    注意:

    设置utf8编码时,不能用utf-8,而是utf8(因为在mysql中设置编码不能包含-)

    Note: Possible UTF-8 confusion

    Because character set names in MySQL do not contain dashes, the string "utf8" is valid in MySQL to set the character set to UTF-8. The string "utf-8" is not valid, as using "utf-8" will fail to change the character set.

  • 相关阅读:
    vuecli3.0 优雅使用svg
    解决vueRouter 跳转相同路由报错
    vue项目中使用 web worker 开启多线程
    项目经常使用的common.js 方法
    vue实现3D词云
    怎么禁止用户点击f12查看数据,查看网页代码,布局样式
    1:vue项目优化总结
    手写一个发布订阅
    vue3.0 reactive 理解
    ifram父子组件传参
  • 原文地址:https://www.cnblogs.com/Alight/p/5082812.html
Copyright © 2020-2023  润新知