zoukankan      html  css  js  c++  java
  • php基础系列:PHP连接MySQL数据库用到的三种API

    参考自PHP手册。本文没有太大意义,仅为方便自己上网查阅。

    1、PHP的MySQL扩展
    2、PHP的mysqli扩展
    3、PHP数据对象(PDO)

    MySQL扩展函数
           这是设计开发允许PHP应用与MySQL数据库交互的早期扩展。mysql扩展提供了一个面向过程  的接口,并且是针对MySQL4.1.3或更早版本设计的。因此,这个扩展虽然可以与MySQL4.1.3或更新的数据库服务端  进行交互,但并不支持后期MySQL服务端提供的一些特性。

    • mysql_affected_rows — 取得前一次 MySQL 操作所影响的记录行数
    • mysql_client_encoding — 返回字符集的名称
    • mysql_close — 关闭 MySQL 连接
    • mysql_connect — 打开一个到 MySQL 服务器的连接
    • mysql_create_db — 新建一个 MySQL 数据库
    • mysql_data_seek — 移动内部结果的指针
    • mysql_db_name — 取得结果数据
    • mysql_db_query — 发送一条 MySQL 查询
    • mysql_drop_db — 丢弃(删除)一个 MySQL 数据库
    • mysql_errno — 返回上一个 MySQL 操作中的错误信息的数字编码
    • mysql_error — 返回上一个 MySQL 操作产生的文本错误信息
    • mysql_escape_string — 转义一个字符串用于 mysql_query
    • mysql_fetch_array — 从结果集中取得一行作为关联数组,或数字数组,或二者兼有
    • mysql_fetch_assoc — 从结果集中取得一行作为关联数组
    • mysql_fetch_field — 从结果集中取得列信息并作为对象返回
    • mysql_fetch_lengths — 取得结果集中每个输出的长度
    • mysql_fetch_object — 从结果集中取得一行作为对象
    • mysql_fetch_row — 从结果集中取得一行作为枚举数组
    • mysql_field_flags — 从结果中取得和指定字段关联的标志
    • mysql_field_len — 返回指定字段的长度
    • mysql_field_name — 取得结果中指定字段的字段名
    • mysql_field_seek — 将结果集中的指针设定为制定的字段偏移量
    • mysql_field_table — 取得指定字段所在的表名
    • mysql_field_type — 取得结果集中指定字段的类型
    • mysql_free_result — 释放结果内存
    • mysql_get_client_info — 取得 MySQL 客户端信息
    • mysql_get_host_info — 取得 MySQL 主机信息
    • mysql_get_proto_info — 取得 MySQL 协议信息
    • mysql_get_server_info — 取得 MySQL 服务器信息
    • mysql_info — 取得最近一条查询的信息
    • mysql_insert_id — 取得上一步 INSERT 操作产生的 ID
    • mysql_list_dbs — 列出 MySQL 服务器中所有的数据库
    • mysql_list_fields — 列出 MySQL 结果中的字段
    • mysql_list_processes — 列出 MySQL 进程
    • mysql_list_tables — 列出 MySQL 数据库中的表
    • mysql_num_fields — 取得结果集中字段的数目
    • mysql_num_rows — 取得结果集中行的数目
    • mysql_pconnect — 打开一个到 MySQL 服务器的持久连接
    • mysql_ping — Ping 一个服务器连接,如果没有连接则重新连接
    • mysql_query — 发送一条 MySQL 查询
    • mysql_real_escape_string — 转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集
    • mysql_result — 取得结果数据
    • mysql_select_db — 选择 MySQL 数据库
    • mysql_set_charset — Sets the client character set
    • mysql_stat — 取得当前系统状态
    • mysql_tablename — 取得表名
    • mysql_thread_id — 返回当前线程的 ID
    • mysql_unbuffered_query — 向 MySQL 发送一条 SQL 查询,并不获取和缓存结果的行

    PHP的mysqli扩展
          mysqli扩展,我们有时称之为MySQL增强扩展,可以用于使用   MySQL4.1.3或更新版本中新的高级特性。mysqli扩展在PHP 5及以后版本中包含。

          面向对象接口
          prepared语句支持
          多语句执行支持
         事务支持
         增强的调试能力
         嵌入式服务支持
         如果你使用MySQL4.1.3或更新版本,强烈建议你使用这个扩展。
         MySQLi           {

    /* 属性 */
    int $MySQLi->affected_rows;
    string $client_info;
    int $client_version;
    string $connect_errno;
    string $connect_error;
    int $errno;
    string $error;
    int $field_count;
    int $client_version;
    string $host_info;
    string $protocol_version;
    string $server_info;
    int $server_version;
    string $info;
    mixed $insert_id;
    string $sqlstate;
    int $thread_id;
    int $warning_count;
    /* 方法 */
    int mysqli_affected_rows    ( mysqli$link   )
    bool mysqli::autocommit    ( bool$mode   )
    bool mysqli::change_user    ( string$user   ,string$password   ,string$database   )
    string mysqli::character_set_name    ( void   )
    string mysqli_get_client_info    ( mysqli$link   )
    int mysqli_get_client_version    ( mysqli$link   )
    bool mysqli::close    ( void   )
    bool mysqli::commit    ( void   )
    int mysqli_connect_errno    ( void   )
    string mysqli_connect_error    ( void   )
    mysqli mysqli_connect    ([ string$host = ini_get("mysqli.default_host")   [,string$username = ini_get("mysqli.default_user")   [,string$passwd = ini_get("mysqli.default_pw")   [,string$dbname = ""   [,int $port = ini_get("mysqli.default_port")   [,string$socket = ini_get("mysqli.default_socket")  ]]]]]] )
    bool mysqli::debug    ( string$message   )
    bool mysqli::dump_debug_info    ( void   )
    int mysqli_errno    ( mysqli$link   )
    string mysqli_error    ( mysqli$link   )
    int mysqli_field_count    ( mysqli$link   )
    object mysqli::get_charset    ( void   )
    string mysqli::get_client_info    ( void   )
    array mysqli_get_client_stats    ( void   )
    int mysqli_get_client_version    ( mysqli$link   )
    bool mysqli::get_connection_stats    ( void   )
    string mysqli_get_host_info    ( mysqli$link   )
    int mysqli_get_proto_info    ( mysqli$link   )
    string mysqli_get_server_info    ( mysqli$link   )
    int mysqli_get_server_version    ( mysqli$link   )
    mysqli_warningmysqli::get_warnings    (void   )
    string mysqli_info    ( mysqli$link   )
    mysqli mysqli::init    ( void   )
    mixed mysqli_insert_id    ( mysqli$link   )
    bool mysqli::kill    ( int $processid   )
    bool mysqli::more_results    ( void   )
    bool mysqli::multi_query    ( string$query   )
    bool mysqli::next_result    ( void   )
    bool mysqli::options    ( int$option   ,mixed$value   )
    bool mysqli::ping    ( void   )
    public int mysqli::poll    ( array &$read   ,array&$error   , array&$reject   ,int $sec   [,int $usec  ] )
    mysqli_stmt mysqli::prepare    ( string$query   )
    mixed mysqli::query    ( string$query   [,int$resultmode  ] )
    bool mysqli::real_connect    ([ string$host   [,string$username   [,string$passwd   [,string$dbname   [,int$port   [,string$socket   [,int$flags  ]]]]]]] )
    string mysqli::escape_string    ( string$escapestr   )
    bool mysqli::real_query      ( string$query    )
    public mysqli_result mysqli::reap_async_query    (void   )
    bool mysqli::rollback    ( void   )
    bool mysqli::select_db    ( string$dbname   )
    bool mysqli::set_charset    ( string$charset   )
    void mysqli_set_local_infile_default    ( mysqli$link   )
    bool mysqli::set_local_infile_handler    ( mysqli$link   ,callback$read_func   )
    string mysqli_sqlstate    ( mysqli$link   )
    bool mysqli::ssl_set    ( string$key   ,string$cert   ,string$ca   ,string$capath   ,string$cipher   )
    string mysqli::stat    ( void   )
    mysqli_stmt mysqli::stmt_init    ( void   )
    mysqli_resultmysqli::store_result    (void   )
    int mysqli_thread_id    ( mysqli$link   )
    bool mysqli_thread_safe    ( void   )
    mysqli_resultmysqli::use_result    (void   )
    int mysqli_warning_count    ( mysqli$link   )

       }

    Table of Contents

    • mysqli->affected_rows — Gets the number of affected rows in a previous MySQL operation
    • mysqli::autocommit — Turns on or off auto-commiting database modifications
    • mysqli::change_user — Changes the user of the specified database connection
    • mysqli::character_set_name — Returns the default character set for the database connection
    • mysqli->client_info — Returns the MySQL client version as a string
    • mysqli->client_version — Get MySQL client info
    • mysqli::close — Closes a previously opened database connection
    • mysqli::commit — Commits the current transaction
    • mysqli->connect_errno — Returns the error code from last connect call
    • mysqli->connect_error — Returns a string description of the last connect error
    • mysqli::__construct — Open a new connection to the MySQL server
    • mysqli::debug — Performs debugging operations
    • mysqli::dump_debug_info — Dump debugging information into the log
    • mysqli->errno — Returns the error code for the most recent function call
    • mysqli->error — Returns a string description of the last error
    • mysqli->field_count — Returns the number of columns for the most recent query
    • mysqli::get_charset — Returns a character set object
    • mysqli->get_client_info — Returns the MySQL client version as a string
    • mysqli_get_client_stats — Returns client per-process statistics
    • mysqli->client_version — Get MySQL client info
    • mysqli::get_connection_stats — Returns statistics about the client connection
    • mysqli->host_info — Returns a string representing the type of connection used
    • mysqli->protocol_version — Returns the version of the MySQL protocol used
    • mysqli->server_info — Returns the version of the MySQL server
    • mysqli->server_version — Returns the version of the MySQL server as an integer
    • mysqli::get_warnings — Get result of SHOW WARNINGS
    • mysqli->info — Retrieves information about the most recently executed query
    • mysqli::init — Initializes MySQLi and returns a resource for use with mysqli_real_connect()
    • mysqli->insert_id — Returns the auto generated id used in the last query
    • mysqli::kill — Asks the server to kill a MySQL thread
    • mysqli::more_results — Check if there are any more query results from a multi query
    • mysqli::multi_query — Performs a query on the database
    • mysqli::next_result — Prepare next result from multi_query
    • mysqli::options — Set options
    • mysqli::ping — Pings a server connection, or tries to reconnect if the connection has gone down
    • mysqli::poll — Poll connections
    • mysqli::prepare — Prepare an SQL statement for execution
    • mysqli::query — Performs a query on the database
    • mysqli::real_connect — Opens a connection to a mysql server
    • mysqli::real_escape_string — Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection
    • mysqli::real_query — Execute an SQL query
    • mysqli::reap_async_query — Get result from async query
    • mysqli::rollback — Rolls back current transaction
    • mysqli::select_db — Selects the default database for database queries
    • mysqli::set_charset — Sets the default client character set
    • mysqli::set_local_infile_default — Unsets user defined handler for load local infile command
    • mysqli::set_local_infile_handler — Set callback function for LOAD DATA LOCAL INFILE command
    • mysqli->sqlstate — Returns the SQLSTATE error from previous MySQL operation
    • mysqli::ssl_set — Used for establishing secure connections using SSL
    • mysqli::stat — Gets the current system status
    • mysqli::stmt_init — Initializes a statement and returns an object for use with mysqli_stmt_prepare
    • mysqli::store_result — Transfers a result set from the last query
    • mysqli->thread_id — Returns the thread ID for the current connection
    • mysqli::thread_safe — Returns whether thread safety is given or not
    • mysqli::use_result — Initiate a result set retrieval
    • mysqli->warning_count — Returns the number of warnings from the last query for the given link



    PHP数据对象(PDO)
          PHP数据对象,是PHP应用中的一个数据库抽象层规范。PDO提供了一个统一的API接口可以使得你的PHP应用不去关心具体要  连接的数据库服务器系统类型。也就是说,如果你使用PDO的API,可以在任何需要的时候无缝切换数据库服务器,比如从Firebird   到MySQL,仅仅需要修改很少的PHP代码。
         当然,PDO也有它自己的先进性,比如一个干净的,简单的,可移植的API,它最主要的缺点是会限制让你不能使用  后期MySQL服务端提供所有的数据库高级特性。比如,PDO不允许使用MySQL支持的多语句执行。
         PDO的MySQL驱动并不是一套API,至少从PHP程序员的角度来看是这样的。实际上,PDO的MySQL驱动处于PDO自己的下层,  提供了特定的Mysql功能。程序员直接调用PDO的API,而PDO使用了PDO的MySQL驱动完成与MySQL服务器端的交互。  
         PDO的MySQL驱动是众多PDO驱动中的一个。其他可用的PDO驱动包括Firebird,PostgreSQL等等。 

            PDO           {

    __construct    (string$dsn   [, string$username   [, string$password   [,array$driver_options  ]]] )
    bool beginTransaction    ( void   )
    bool commit    ( void   )
    mixed errorCode    ( void   )
    array errorInfo    ( void   )
    int exec    ( string $statement   )
    mixed getAttribute    ( int $attribute   )
    array getAvailableDrivers    ( void   )
    bool inTransaction    ( void   )
    string lastInsertId    ([ string$name =NULL  ] )
    PDOStatementprepare    (string$statement   [,array$driver_options = array()  ] )
    PDOStatementquery    (string$statement   )
    string quote    ( string $string   [, int $parameter_type = PDO::PARAM_STR  ] )
    bool rollBack    ( void   )
    bool setAttribute    ( int $attribute   , mixed$value   )

        }

    Table of Contents

      • PDO::beginTransaction — Initiates a transaction
      • PDO::commit — Commits a transaction
      • PDO::__construct — Creates a PDO instance representing a connection to a database
      • PDO::errorCode — Fetch the SQLSTATE associated with the last operation on the database handle
      • PDO::errorInfo — Fetch extended error information associated with the last operation on the database handle
      • PDO::exec — Execute an SQL statement and return the number of affected rows
      • PDO::getAttribute — Retrieve a database connection attribute
      • PDO::getAvailableDrivers — Return an array of available PDO drivers
      • PDO::inTransaction — Checks if inside a transaction
      • PDO::lastInsertId — Returns the ID of the last inserted row or sequence value
      • PDO::prepare — Prepares a statement for execution and returns a statement object
      • PDO::query — Executes an SQL statement, returning a result set as a PDOStatement object
      • PDO::quote — Quotes a string for use in a query.
      • PDO::rollBack — Rolls back a transaction
      • PDO::setAttribute — Set an attribute
  • 相关阅读:
    进度报告(五)
    大道至简读后感
    进度报告(四)
    进度报告(三)
    进度报告(二)
    进度报告(一)
    18.4---2出现了几次(CC150)
    17.5---珠玑妙算,四个槽,红色黄色绿色蓝色(CC150)
    17.2---#字棋(CC150)
    17.3---阶乘尾多少个0(CC150)
  • 原文地址:https://www.cnblogs.com/rxbook/p/6024401.html
Copyright © 2011-2022 走看看