PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/mysql - php_mysql.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 773 840 92.0 %
Date: 2014-10-14 Functions: 62 64 96.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Zeev Suraski <zeev@zend.com>                                |
      16             :    |          Zak Greant <zak@mysql.com>                                  |
      17             :    |          Georg Richter <georg@php.net>                               |
      18             :    +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : /* TODO:
      24             :  *
      25             :  * ? Safe mode implementation
      26             :  */
      27             : 
      28             : #ifdef HAVE_CONFIG_H
      29             : # include "config.h"
      30             : #endif
      31             : 
      32             : #include "php.h"
      33             : #include "php_globals.h"
      34             : #include "ext/standard/info.h"
      35             : #include "ext/standard/php_string.h"
      36             : #include "ext/standard/basic_functions.h"
      37             : 
      38             : #ifdef ZEND_ENGINE_2
      39             : # include "zend_exceptions.h"
      40             : #else
      41             :   /* PHP 4 compat */
      42             : # define OnUpdateLong   OnUpdateInt
      43             : # define E_STRICT               E_NOTICE
      44             : #endif
      45             : 
      46             : #if HAVE_MYSQL
      47             : 
      48             : #ifdef PHP_WIN32
      49             : # include <winsock2.h>
      50             : # define signal(a, b) NULL
      51             : #elif defined(NETWARE)
      52             : # include <sys/socket.h>
      53             : # define signal(a, b) NULL
      54             : #else
      55             : # if HAVE_SIGNAL_H
      56             : #  include <signal.h>
      57             : # endif
      58             : # if HAVE_SYS_TYPES_H
      59             : #  include <sys/types.h>
      60             : # endif
      61             : # include <netdb.h>
      62             : # include <netinet/in.h>
      63             : # if HAVE_ARPA_INET_H
      64             : #  include <arpa/inet.h>
      65             : # endif
      66             : #endif
      67             : 
      68             : #include "php_ini.h"
      69             : #include "php_mysql_structs.h"
      70             : 
      71             : /* True globals, no need for thread safety */
      72             : static int le_result, le_link, le_plink;
      73             : 
      74             : #ifdef HAVE_MYSQL_REAL_CONNECT
      75             : # ifdef HAVE_ERRMSG_H
      76             : #  include <errmsg.h>
      77             : # endif
      78             : #endif
      79             : 
      80             : #define SAFE_STRING(s) ((s)?(s):"")
      81             : 
      82             : #if MYSQL_VERSION_ID > 32199 || defined(MYSQL_USE_MYSQLND)
      83             : # define mysql_row_length_type zend_ulong
      84             : # define HAVE_MYSQL_ERRNO
      85             : #else
      86             : # define mysql_row_length_type unsigned int
      87             : # ifdef mysql_errno
      88             : #  define HAVE_MYSQL_ERRNO
      89             : # endif
      90             : #endif
      91             : 
      92             : #if MYSQL_VERSION_ID >= 32032 || defined(MYSQL_USE_MYSQLND)
      93             : #define HAVE_GETINFO_FUNCS
      94             : #endif
      95             : 
      96             : #if MYSQL_VERSION_ID > 32133 || defined(FIELD_TYPE_TINY)
      97             : #define MYSQL_HAS_TINY
      98             : #endif
      99             : 
     100             : #if MYSQL_VERSION_ID >= 32200
     101             : #define MYSQL_HAS_YEAR
     102             : #endif
     103             : 
     104             : #define MYSQL_ASSOC             1<<0
     105             : #define MYSQL_NUM               1<<1
     106             : #define MYSQL_BOTH              (MYSQL_ASSOC|MYSQL_NUM)
     107             : 
     108             : #define MYSQL_USE_RESULT        0
     109             : #define MYSQL_STORE_RESULT      1
     110             : 
     111             : #if MYSQL_VERSION_ID < 32224
     112             : #define PHP_MYSQL_VALID_RESULT(mysql)           \
     113             :         (mysql_num_fields(mysql)>0)
     114             : #else
     115             : #define PHP_MYSQL_VALID_RESULT(mysql)           \
     116             :         (mysql_field_count(mysql)>0)
     117             : #endif
     118             : 
     119             : ZEND_DECLARE_MODULE_GLOBALS(mysql)
     120             : static PHP_GINIT_FUNCTION(mysql);
     121             : 
     122             : typedef struct _php_mysql_conn {
     123             :         MYSQL *conn;
     124             :         zend_resource *active_result_res;
     125             :         int multi_query;
     126             : } php_mysql_conn;
     127             : 
     128             : 
     129             : #if MYSQL_VERSION_ID >= 40101
     130             : #define MYSQL_DISABLE_MQ if (mysql->multi_query) { \
     131             :         mysql_set_server_option(mysql->conn, MYSQL_OPTION_MULTI_STATEMENTS_OFF); \
     132             :         mysql->multi_query = 0; \
     133             : }
     134             : #else
     135             : #define MYSQL_DISABLE_MQ
     136             : #endif
     137             : 
     138             : /* {{{ arginfo */
     139             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_connect, 0, 0, 0)
     140             :         ZEND_ARG_INFO(0, hostname)
     141             :         ZEND_ARG_INFO(0, username)
     142             :         ZEND_ARG_INFO(0, password)
     143             :         ZEND_ARG_INFO(0, new)
     144             :         ZEND_ARG_INFO(0, flags)
     145             : ZEND_END_ARG_INFO()
     146             : 
     147             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_pconnect, 0, 0, 0)
     148             :         ZEND_ARG_INFO(0, hostname)
     149             :         ZEND_ARG_INFO(0, username)
     150             :         ZEND_ARG_INFO(0, password)
     151             :         ZEND_ARG_INFO(0, flags)
     152             : ZEND_END_ARG_INFO()
     153             : 
     154             : ZEND_BEGIN_ARG_INFO_EX(arginfo__optional_mysql_link, 0, 0, 0)
     155             :         ZEND_ARG_INFO(0, link_identifier)
     156             : ZEND_END_ARG_INFO()
     157             : 
     158             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_select_db, 0, 0, 1)
     159             :         ZEND_ARG_INFO(0, database_name)
     160             :         ZEND_ARG_INFO(0, link_identifier)
     161             : ZEND_END_ARG_INFO()
     162             : 
     163             : ZEND_BEGIN_ARG_INFO(arginfo__void_mysql_arg, 0)
     164             : ZEND_END_ARG_INFO()
     165             : 
     166             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_set_charset, 0, 0, 1)
     167             :         ZEND_ARG_INFO(0, charset_name)
     168             :         ZEND_ARG_INFO(0, link_identifier)
     169             : ZEND_END_ARG_INFO()
     170             : 
     171             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_query, 0, 0, 1)
     172             :         ZEND_ARG_INFO(0, query)
     173             :         ZEND_ARG_INFO(0, link_identifier)
     174             : ZEND_END_ARG_INFO()
     175             : 
     176             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_db_query, 0, 0, 2)
     177             :         ZEND_ARG_INFO(0, database_name)
     178             :         ZEND_ARG_INFO(0, query)
     179             :         ZEND_ARG_INFO(0, link_identifier)
     180             : ZEND_END_ARG_INFO()
     181             : 
     182             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_list_fields, 0, 0, 2)
     183             :         ZEND_ARG_INFO(0, database_name)
     184             :         ZEND_ARG_INFO(0, table_name)
     185             :         ZEND_ARG_INFO(0, link_identifier)
     186             : ZEND_END_ARG_INFO()
     187             : 
     188             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_escape_string, 0, 0, 1)
     189             :         ZEND_ARG_INFO(0, string)
     190             : ZEND_END_ARG_INFO()
     191             : 
     192             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_real_escape_string, 0, 0, 1)
     193             :         ZEND_ARG_INFO(0, string)
     194             :         ZEND_ARG_INFO(0, link_identifier)
     195             : ZEND_END_ARG_INFO()
     196             : 
     197             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_result, 0, 0, 2)
     198             :         ZEND_ARG_INFO(0, result)
     199             :         ZEND_ARG_INFO(0, row)
     200             :         ZEND_ARG_INFO(0, field)
     201             : ZEND_END_ARG_INFO()
     202             : 
     203             : ZEND_BEGIN_ARG_INFO_EX(arginfo__result_mysql_arg, 0, 0, 1)
     204             :         ZEND_ARG_INFO(0, result)
     205             : ZEND_END_ARG_INFO()
     206             : 
     207             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_fetch_object, 0, 0, 1)
     208             :         ZEND_ARG_INFO(0, result)
     209             :         ZEND_ARG_INFO(0, class_name)
     210             :         ZEND_ARG_INFO(0, ctor_params)
     211             : ZEND_END_ARG_INFO()
     212             : 
     213             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_fetch_array, 0, 0, 1)
     214             :         ZEND_ARG_INFO(0, result)
     215             :         ZEND_ARG_INFO(0, result_type)
     216             : ZEND_END_ARG_INFO()
     217             : 
     218             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_data_seek, 0, 0, 2)
     219             :         ZEND_ARG_INFO(0, result)
     220             :         ZEND_ARG_INFO(0, row_number)
     221             : ZEND_END_ARG_INFO()
     222             : 
     223             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_fetch_field, 0, 0, 1)
     224             :         ZEND_ARG_INFO(0, result)
     225             :         ZEND_ARG_INFO(0, field_offset)
     226             : ZEND_END_ARG_INFO()
     227             : 
     228             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_field_seek, 0, 0, 2)
     229             :         ZEND_ARG_INFO(0, result)
     230             :         ZEND_ARG_INFO(0, field_offset)
     231             : ZEND_END_ARG_INFO()
     232             : 
     233             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_field_name, 0, 0, 2)
     234             :         ZEND_ARG_INFO(0, result)
     235             :         ZEND_ARG_INFO(0, field_index)
     236             : ZEND_END_ARG_INFO()
     237             : /* }}} */
     238             : 
     239             : /* {{{ mysql_functions[]
     240             :  */
     241             : static const zend_function_entry mysql_functions[] = {
     242             :         PHP_FE(mysql_connect,                                                           arginfo_mysql_connect)
     243             :         PHP_FE(mysql_pconnect,                                                          arginfo_mysql_pconnect)
     244             :         PHP_FE(mysql_close,                                                                     arginfo__optional_mysql_link)
     245             :         PHP_FE(mysql_select_db,                                                         arginfo_mysql_select_db)
     246             : #ifndef NETWARE         /* The below two functions not supported on NetWare */
     247             : #if MYSQL_VERSION_ID < 40000
     248             :         PHP_DEP_FE(mysql_create_db,                                                     arginfo_mysql_select_db)
     249             :         PHP_DEP_FE(mysql_drop_db,                                                       arginfo_mysql_select_db)
     250             : #endif
     251             : #endif  /* NETWARE */
     252             :         PHP_FE(mysql_query,                                                                     arginfo_mysql_query)
     253             :         PHP_FE(mysql_unbuffered_query,                                          arginfo_mysql_query)
     254             :         PHP_DEP_FE(mysql_db_query,                                                      arginfo_mysql_db_query)
     255             :         PHP_DEP_FE(mysql_list_dbs,                                                      arginfo__optional_mysql_link)
     256             :         PHP_DEP_FE(mysql_list_tables,                                           arginfo_mysql_select_db)
     257             :         PHP_FE(mysql_list_fields,                                                       arginfo_mysql_list_fields)
     258             :         PHP_FE(mysql_list_processes,                                            arginfo__optional_mysql_link)
     259             :         PHP_FE(mysql_error,                                                                     arginfo__optional_mysql_link)
     260             : #ifdef HAVE_MYSQL_ERRNO
     261             :         PHP_FE(mysql_errno,                                                                     arginfo__optional_mysql_link)
     262             : #endif
     263             :         PHP_FE(mysql_affected_rows,                                                     arginfo__optional_mysql_link)
     264             :         PHP_FE(mysql_insert_id,                                                         arginfo__optional_mysql_link)
     265             :         PHP_FE(mysql_result,                                                            arginfo_mysql_result)
     266             :         PHP_FE(mysql_num_rows,                                                          arginfo__result_mysql_arg)
     267             :         PHP_FE(mysql_num_fields,                                                        arginfo__result_mysql_arg)
     268             :         PHP_FE(mysql_fetch_row,                                                         arginfo__result_mysql_arg)
     269             :         PHP_FE(mysql_fetch_array,                                                       arginfo_mysql_fetch_array)
     270             :         PHP_FE(mysql_fetch_assoc,                                                       arginfo__result_mysql_arg)
     271             :         PHP_FE(mysql_fetch_object,                                                      arginfo_mysql_fetch_object)
     272             :         PHP_FE(mysql_data_seek,                                                         arginfo_mysql_data_seek)
     273             :         PHP_FE(mysql_fetch_lengths,                                                     arginfo__result_mysql_arg)
     274             :         PHP_FE(mysql_fetch_field,                                                       arginfo_mysql_fetch_field)
     275             :         PHP_FE(mysql_field_seek,                                                        arginfo_mysql_field_seek)
     276             :         PHP_FE(mysql_free_result,                                                       arginfo__result_mysql_arg)
     277             :         PHP_FE(mysql_field_name,                                                        arginfo_mysql_field_name)
     278             :         PHP_FE(mysql_field_table,                                                       arginfo_mysql_field_seek)
     279             :         PHP_FE(mysql_field_len,                                                         arginfo_mysql_field_seek)
     280             :         PHP_FE(mysql_field_type,                                                        arginfo_mysql_field_seek)
     281             :         PHP_FE(mysql_field_flags,                                                       arginfo_mysql_field_seek)
     282             :         PHP_FE(mysql_escape_string,                                                     arginfo_mysql_escape_string)
     283             :         PHP_FE(mysql_real_escape_string,                                        arginfo_mysql_real_escape_string)
     284             :         PHP_FE(mysql_stat,                                                                      arginfo__optional_mysql_link)
     285             :         PHP_FE(mysql_thread_id,                                                         arginfo__optional_mysql_link)
     286             :         PHP_FE(mysql_client_encoding,                                           arginfo__optional_mysql_link)
     287             :         PHP_FE(mysql_ping,                                                                      arginfo__optional_mysql_link)
     288             : #ifdef HAVE_GETINFO_FUNCS
     289             :         PHP_FE(mysql_get_client_info,                                           arginfo__void_mysql_arg)
     290             :         PHP_FE(mysql_get_host_info,                                                     arginfo__optional_mysql_link)
     291             :         PHP_FE(mysql_get_proto_info,                                            arginfo__optional_mysql_link)
     292             :         PHP_FE(mysql_get_server_info,                                           arginfo__optional_mysql_link)
     293             : #endif
     294             : 
     295             :         PHP_FE(mysql_info,                                                                      arginfo__optional_mysql_link)
     296             : #ifdef MYSQL_HAS_SET_CHARSET
     297             :         PHP_FE(mysql_set_charset,                                                       arginfo_mysql_set_charset)
     298             : #endif
     299             :         /* for downwards compatibility */
     300             :         PHP_DEP_FALIAS(mysql,                           mysql_db_query,         arginfo_mysql_db_query)
     301             :         PHP_DEP_FALIAS(mysql_fieldname,         mysql_field_name,       arginfo_mysql_field_name)
     302             :         PHP_DEP_FALIAS(mysql_fieldtable,        mysql_field_table,      arginfo_mysql_field_seek)
     303             :         PHP_DEP_FALIAS(mysql_fieldlen,          mysql_field_len,        arginfo_mysql_field_seek)
     304             :         PHP_DEP_FALIAS(mysql_fieldtype,         mysql_field_type,       arginfo_mysql_field_seek)
     305             :         PHP_DEP_FALIAS(mysql_fieldflags,        mysql_field_flags,      arginfo_mysql_field_seek)
     306             :         PHP_DEP_FALIAS(mysql_selectdb,          mysql_select_db,        arginfo_mysql_select_db)
     307             : #ifndef NETWARE         /* The below two functions not supported on NetWare */
     308             : #if MYSQL_VERSION_ID < 40000
     309             :         PHP_DEP_FALIAS(mysql_createdb,  mysql_create_db,        arginfo_mysql_select_db)
     310             :         PHP_DEP_FALIAS(mysql_dropdb,    mysql_drop_db,          arginfo_mysql_select_db)
     311             : #endif
     312             : #endif  /* NETWARE */
     313             :         PHP_DEP_FALIAS(mysql_freeresult,        mysql_free_result,      arginfo__result_mysql_arg)
     314             :         PHP_DEP_FALIAS(mysql_numfields,         mysql_num_fields,       arginfo__result_mysql_arg)
     315             :         PHP_DEP_FALIAS(mysql_numrows,           mysql_num_rows,         arginfo__result_mysql_arg)
     316             :         PHP_DEP_FALIAS(mysql_listdbs,           mysql_list_dbs,         arginfo__optional_mysql_link)
     317             :         PHP_DEP_FALIAS(mysql_listtables,mysql_list_tables,      arginfo_mysql_select_db)
     318             :         PHP_DEP_FALIAS(mysql_listfields,        mysql_list_fields,      arginfo_mysql_list_fields)
     319             :         PHP_FALIAS(mysql_db_name,               mysql_result,           arginfo_mysql_result)
     320             :         PHP_DEP_FALIAS(mysql_dbname,            mysql_result,           arginfo_mysql_result)
     321             :         PHP_FALIAS(mysql_tablename,             mysql_result,           arginfo_mysql_result)
     322             :         PHP_FALIAS(mysql_table_name,    mysql_result,           arginfo_mysql_result)
     323             :         PHP_FE_END
     324             : };
     325             : /* }}} */
     326             : 
     327             : /* Dependancies */
     328             : static const zend_module_dep mysql_deps[] = {
     329             : #if defined(MYSQL_USE_MYSQLND)
     330             :         ZEND_MOD_REQUIRED("mysqlnd")
     331             : #endif
     332             :         ZEND_MOD_END
     333             : };
     334             : 
     335             : /* {{{ mysql_module_entry
     336             :  */
     337             : zend_module_entry mysql_module_entry = {
     338             : #if ZEND_MODULE_API_NO >= 20050922
     339             :         STANDARD_MODULE_HEADER_EX, NULL,
     340             :         mysql_deps,
     341             : #elif ZEND_MODULE_API_NO >= 20010901
     342             :         STANDARD_MODULE_HEADER,
     343             : #endif
     344             :         "mysql",
     345             :         mysql_functions,
     346             :         ZEND_MODULE_STARTUP_N(mysql),
     347             :         PHP_MSHUTDOWN(mysql),
     348             :         PHP_RINIT(mysql),
     349             :         PHP_RSHUTDOWN(mysql),
     350             :         PHP_MINFO(mysql),
     351             :         "1.0",
     352             :         PHP_MODULE_GLOBALS(mysql),
     353             :         PHP_GINIT(mysql),
     354             :         NULL,
     355             :         NULL,
     356             :         STANDARD_MODULE_PROPERTIES_EX
     357             : };
     358             : /* }}} */
     359             : 
     360             : #ifdef COMPILE_DL_MYSQL
     361             : ZEND_GET_MODULE(mysql)
     362             : #endif
     363             : 
     364             : void timeout(int sig);
     365             : 
     366             : #define CHECK_LINK(link) { if (link == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "A link to the server could not be established"); RETURN_FALSE; } }
     367             : 
     368             : #if defined(MYSQL_USE_MYSQLND)
     369             : #define PHPMY_UNBUFFERED_QUERY_CHECK()                                                                  \
     370             : {                                                                                                                                               \
     371             :         if (mysql->active_result_res) {                                                                      \
     372             :                 do {                                                                                                                    \
     373             :                         MYSQL_RES *_mysql_result;                                                                       \
     374             :                         _mysql_result = (MYSQL_RES *)mysql->active_result_res->ptr;       \
     375             :                         if (_mysql_result &&                                                                            \
     376             :                                         mysql->active_result_res->type == le_result) {            \
     377             :                                 if (mysql_result_is_unbuffered(_mysql_result) &&                \
     378             :                                                 !mysql_eof(_mysql_result)) {                                    \
     379             :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE,                      \
     380             :                                                 "Function called without first fetching all " \
     381             :                                                 "rows from a previous unbuffered query");             \
     382             :                                 }                                                                                                               \
     383             :                                 zend_list_close(mysql->active_result_res);                           \
     384             :                                 mysql->active_result_res = NULL;                                             \
     385             :                         }                                                                                                                       \
     386             :                 } while(0);                                                                                                     \
     387             :         }                                                                                                                                       \
     388             : }
     389             : #else
     390             : #define PHPMY_UNBUFFERED_QUERY_CHECK()                                                                  \
     391             : {                                                                                                                                               \
     392             :         if (mysql->active_result_res) {                                                                              \
     393             :                 do {                                                                                                                    \
     394             :                         MYSQL_RES *mysql_result;                                                                        \
     395             :                         mysql_result = (MYSQL_RES *) mysql->active_result_res->ptr;       \
     396             :                         if (mysql_result &&                                                                                     \
     397             :                                         mysql->active_result_res->type == le_result) {            \
     398             :                                 if (!mysql_eof(mysql_result)) {                                                 \
     399             :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE,                      \
     400             :                                                 "Function called without first fetching "             \
     401             :                                                 "all rows from a previous unbuffered query"); \
     402             :                                         while (mysql_fetch_row(mysql_result));                          \
     403             :                                 }                                                                                                               \
     404             :                                 zend_list_close(mysql->active_result_res);                           \
     405             :                                 mysql->active_result_res = NULL;                                             \
     406             :                         }                                                                                                                       \
     407             :                 } while(0);                                                                                                             \
     408             :         }                                                                                                                                       \
     409             : }
     410             : #endif
     411             : 
     412             : /* {{{ _free_mysql_result
     413             :  * This wrapper is required since mysql_free_result() returns an integer, and
     414             :  * thus, cannot be used directly
     415             :  */
     416         206 : static void _free_mysql_result(zend_resource *rsrc TSRMLS_DC)
     417             : {
     418         206 :         MYSQL_RES *mysql_result = (MYSQL_RES *)rsrc->ptr;
     419             : 
     420         206 :         mysql_free_result(mysql_result);
     421         206 :         MySG(result_allocated)--;
     422         206 : }
     423             : /* }}} */
     424             : 
     425             : /* {{{ php_mysql_set_default_link
     426             :  */
     427         207 : static void php_mysql_set_default_link(zend_resource *link TSRMLS_DC)
     428             : {
     429         207 :         if (MySG(default_link) != NULL) {
     430          12 :                 zend_list_delete(MySG(default_link));
     431             :         }
     432         207 :         ++GC_REFCOUNT(link);
     433         207 :         MySG(default_link) = link;
     434         207 : }
     435             : /* }}} */
     436             : 
     437             : /* {{{ php_mysql_select_db
     438             : */
     439         193 : static int php_mysql_select_db(php_mysql_conn *mysql, char *db TSRMLS_DC)
     440             : {
     441         193 :         PHPMY_UNBUFFERED_QUERY_CHECK();
     442             : 
     443         193 :         if (mysql_select_db(mysql->conn, db) != 0) {
     444           2 :                 return 0;
     445             :         } else {
     446         191 :                 return 1;
     447             :         }
     448             : }
     449             : /* }}} */
     450             : 
     451             : /* {{{ _close_mysql_link
     452             :  */
     453         186 : static void _close_mysql_link(zend_resource *rsrc TSRMLS_DC)
     454             : {
     455         186 :         php_mysql_conn *link = (php_mysql_conn *)rsrc->ptr;
     456             :         void (*handler)(int);
     457             : 
     458         186 :         handler = signal(SIGPIPE, SIG_IGN);
     459         186 :         mysql_close(link->conn);
     460         186 :         signal(SIGPIPE, handler);
     461         186 :         efree(link);
     462         186 :         MySG(num_links)--;
     463         186 : }
     464             : /* }}} */
     465             : 
     466             : /* {{{ _close_mysql_plink
     467             :  */
     468           9 : static void _close_mysql_plink(zend_resource *rsrc TSRMLS_DC)
     469             : {
     470           9 :         php_mysql_conn *link = (php_mysql_conn *)rsrc->ptr;
     471             :         void (*handler) (int);
     472             : 
     473           9 :         handler = signal(SIGPIPE, SIG_IGN);
     474           9 :         mysql_close(link->conn);
     475           9 :         signal(SIGPIPE, handler);
     476             : 
     477           9 :         free(link);
     478           9 :         MySG(num_persistent)--;
     479           9 :         MySG(num_links)--;
     480           9 : }
     481             : /* }}} */
     482             : 
     483             : /* {{{ PHP_INI_MH
     484             :  */
     485       20406 : static PHP_INI_MH(OnMySQLPort)
     486             : {
     487       20406 :         if (new_value != NULL) { /* default port */
     488           5 :                 MySG(default_port) = atoi(new_value->val);
     489             :         } else {
     490       20401 :                 MySG(default_port) = -1;
     491             :         }
     492             : 
     493       20406 :         return SUCCESS;
     494             : }
     495             : /* }}} */
     496             : 
     497             : /* {{{ PHP_INI */
     498             : PHP_INI_BEGIN()
     499             :         STD_PHP_INI_BOOLEAN("mysql.allow_persistent", "1",  PHP_INI_SYSTEM,         OnUpdateLong,           allow_persistent,       zend_mysql_globals,             mysql_globals)
     500             :         STD_PHP_INI_ENTRY_EX("mysql.max_persistent",  "-1", PHP_INI_SYSTEM,         OnUpdateLong,           max_persistent,         zend_mysql_globals,             mysql_globals,  display_link_numbers)
     501             :         STD_PHP_INI_ENTRY_EX("mysql.max_links",                       "-1", PHP_INI_SYSTEM,         OnUpdateLong,           max_links,                      zend_mysql_globals,             mysql_globals,  display_link_numbers)
     502             :         STD_PHP_INI_ENTRY("mysql.default_host",                       NULL,   PHP_INI_ALL,            OnUpdateString,         default_host,           zend_mysql_globals,             mysql_globals)
     503             :         STD_PHP_INI_ENTRY("mysql.default_user",                       NULL,   PHP_INI_ALL,            OnUpdateString,         default_user,           zend_mysql_globals,             mysql_globals)
     504             :         STD_PHP_INI_ENTRY("mysql.default_password",           NULL,   PHP_INI_ALL,            OnUpdateString,         default_password,       zend_mysql_globals,             mysql_globals)
     505             :         PHP_INI_ENTRY("mysql.default_port",                           NULL,   PHP_INI_ALL,            OnMySQLPort)
     506             : #ifdef MYSQL_UNIX_ADDR
     507             :         STD_PHP_INI_ENTRY("mysql.default_socket",             MYSQL_UNIX_ADDR,PHP_INI_ALL,OnUpdateStringUnempty,      default_socket, zend_mysql_globals,             mysql_globals)
     508             : #else
     509             :         STD_PHP_INI_ENTRY("mysql.default_socket",             NULL,   PHP_INI_ALL,            OnUpdateStringUnempty,  default_socket, zend_mysql_globals,             mysql_globals)
     510             : #endif
     511             :         STD_PHP_INI_ENTRY("mysql.connect_timeout",            "60", PHP_INI_ALL,            OnUpdateLong,           connect_timeout,        zend_mysql_globals,             mysql_globals)
     512             :         STD_PHP_INI_BOOLEAN("mysql.trace_mode",                       "0",  PHP_INI_ALL,            OnUpdateLong,           trace_mode,             zend_mysql_globals,             mysql_globals)
     513             :         STD_PHP_INI_BOOLEAN("mysql.allow_local_infile",       "1",  PHP_INI_SYSTEM,         OnUpdateLong,           allow_local_infile, zend_mysql_globals,         mysql_globals)
     514             : PHP_INI_END()
     515             : /* }}} */
     516             : 
     517             : /* {{{ PHP_GINIT_FUNCTION
     518             :  */
     519       20401 : static PHP_GINIT_FUNCTION(mysql)
     520             : {
     521       20401 :         mysql_globals->num_persistent = 0;
     522       20401 :         mysql_globals->default_socket = NULL;
     523       20401 :         mysql_globals->default_host = NULL;
     524       20401 :         mysql_globals->default_user = NULL;
     525       20401 :         mysql_globals->default_password = NULL;
     526       20401 :         mysql_globals->connect_errno = 0;
     527       20401 :         mysql_globals->connect_error = NULL;
     528       20401 :         mysql_globals->connect_timeout = 0;
     529       20401 :         mysql_globals->trace_mode = 0;
     530       20401 :         mysql_globals->allow_local_infile = 1;
     531       20401 :         mysql_globals->result_allocated = 0;
     532       20401 : }
     533             : /* }}} */
     534             : 
     535             : #ifdef MYSQL_USE_MYSQLND
     536             : #include "ext/mysqlnd/mysqlnd_reverse_api.h"
     537           0 : static MYSQLND *mysql_convert_zv_to_mysqlnd(zval *zv TSRMLS_DC)
     538             : {
     539             :         php_mysql_conn *mysql;
     540             : 
     541           0 :         if (Z_TYPE_P(zv) != IS_RESOURCE) {
     542             :                 /* Might be nicer to check resource type, too, but ext/mysql is the only one using resources so emitting an error is not to bad, while usually this hook should be silent */
     543           0 :                 return NULL;
     544             :         }
     545             : 
     546           0 :         mysql = zend_fetch_resource(zv TSRMLS_CC, -1, "MySQL-Link", NULL, 2, le_link, le_plink);
     547             : 
     548           0 :         if (!mysql) {
     549           0 :                 return NULL;
     550             :         }
     551             : 
     552           0 :         return mysql->conn;
     553             : }
     554             : 
     555             : static MYSQLND_REVERSE_API mysql_reverse_api = {
     556             :         &mysql_module_entry,
     557             :         mysql_convert_zv_to_mysqlnd
     558             : };
     559             : #endif
     560             : 
     561             : /* {{{ PHP_MINIT_FUNCTION
     562             :  */
     563       20401 : ZEND_MODULE_STARTUP_D(mysql)
     564             : {
     565       20401 :         REGISTER_INI_ENTRIES();
     566       20401 :         le_result = zend_register_list_destructors_ex(_free_mysql_result, NULL, "mysql result", module_number);
     567       20401 :         le_link = zend_register_list_destructors_ex(_close_mysql_link, NULL, "mysql link", module_number);
     568       20401 :         le_plink = zend_register_list_destructors_ex(NULL, _close_mysql_plink, "mysql link persistent", module_number);
     569       20401 :         mysql_module_entry.type = type;
     570             : 
     571       20401 :         REGISTER_LONG_CONSTANT("MYSQL_ASSOC", MYSQL_ASSOC, CONST_CS | CONST_PERSISTENT);
     572       20401 :         REGISTER_LONG_CONSTANT("MYSQL_NUM", MYSQL_NUM, CONST_CS | CONST_PERSISTENT);
     573       20401 :         REGISTER_LONG_CONSTANT("MYSQL_BOTH", MYSQL_BOTH, CONST_CS | CONST_PERSISTENT);
     574       20401 :         REGISTER_LONG_CONSTANT("MYSQL_CLIENT_COMPRESS", CLIENT_COMPRESS, CONST_CS | CONST_PERSISTENT);
     575             : #if MYSQL_VERSION_ID >= 40000
     576       20401 :         REGISTER_LONG_CONSTANT("MYSQL_CLIENT_SSL", CLIENT_SSL, CONST_CS | CONST_PERSISTENT);
     577             : #endif
     578       20401 :         REGISTER_LONG_CONSTANT("MYSQL_CLIENT_INTERACTIVE", CLIENT_INTERACTIVE, CONST_CS | CONST_PERSISTENT);
     579       20401 :         REGISTER_LONG_CONSTANT("MYSQL_CLIENT_IGNORE_SPACE", CLIENT_IGNORE_SPACE, CONST_CS | CONST_PERSISTENT);
     580             : 
     581             : #ifndef MYSQL_USE_MYSQLND
     582             : #if MYSQL_VERSION_ID >= 40000
     583             :         if (mysql_server_init(0, NULL, NULL)) {
     584             :                 return FAILURE;
     585             :         }
     586             : #endif
     587             : #endif
     588             : 
     589             : #ifdef MYSQL_USE_MYSQLND
     590       20401 :         mysqlnd_reverse_api_register_api(&mysql_reverse_api TSRMLS_CC);
     591             : #endif
     592             : 
     593       20401 :         return SUCCESS;
     594             : }
     595             : /* }}} */
     596             : 
     597             : /* {{{ PHP_MSHUTDOWN_FUNCTION
     598             :  */
     599       20435 : PHP_MSHUTDOWN_FUNCTION(mysql)
     600             : {
     601             : #ifndef MYSQL_USE_MYSQLND
     602             : #if MYSQL_VERSION_ID >= 40000
     603             : #ifdef PHP_WIN32
     604             :         unsigned long client_ver = mysql_get_client_version();
     605             :         /*
     606             :           Can't call mysql_server_end() multiple times prior to 5.0.46 on Windows.
     607             :           PHP bug#41350 MySQL bug#25621
     608             :         */
     609             :         if ((client_ver >= 50046 && client_ver < 50100) || client_ver > 50122) {
     610             :                 mysql_server_end();
     611             :         }
     612             : #else
     613             :         mysql_server_end();
     614             : #endif
     615             : #endif
     616             : #endif
     617             : 
     618       20435 :         UNREGISTER_INI_ENTRIES();
     619       20435 :         return SUCCESS;
     620             : }
     621             : /* }}} */
     622             : 
     623             : /* {{{ PHP_RINIT_FUNCTION
     624             :  */
     625       20358 : PHP_RINIT_FUNCTION(mysql)
     626             : {
     627             : #if !defined(MYSQL_USE_MYSQLND) && defined(ZTS) && MYSQL_VERSION_ID >= 40000
     628             :         if (mysql_thread_init()) {
     629             :                 return FAILURE;
     630             :         }
     631             : #endif
     632       20358 :         MySG(default_link) = NULL;
     633       20358 :         MySG(num_links) = MySG(num_persistent);
     634             :         /* Reset connect error/errno on every request */
     635       20358 :         MySG(connect_error) = NULL;
     636       20358 :         MySG(connect_errno) =0;
     637       20358 :         MySG(result_allocated) = 0;
     638             : 
     639       20358 :         return SUCCESS;
     640             : }
     641             : /* }}} */
     642             : 
     643             : #if defined(A0) && defined(MYSQL_USE_MYSQLND)
     644             : static int php_mysql_persistent_helper(zval *el TSRMLS_DC)
     645             : {
     646             :         zend_resource *le = (zend_resource *)Z_PTR_P(el);
     647             :         if (le->type == le_plink) {
     648             :                 mysqlnd_end_psession(((php_mysql_conn *) le->ptr)->conn);
     649             :         }
     650             :         return ZEND_HASH_APPLY_KEEP;
     651             : } /* }}} */
     652             : #endif
     653             : 
     654             : 
     655             : /* {{{ PHP_RSHUTDOWN_FUNCTION
     656             :  */
     657       20394 : PHP_RSHUTDOWN_FUNCTION(mysql)
     658             : {
     659             : #if !defined(MYSQL_USE_MYSQLND) && defined(ZTS) && MYSQL_VERSION_ID >= 40000
     660             :         mysql_thread_end();
     661             : #endif
     662             : 
     663       20394 :         if (MySG(trace_mode)) {
     664           4 :                 if (MySG(result_allocated)){
     665           1 :                         php_error_docref("function.mysql-free-result" TSRMLS_CC, E_WARNING, "%pu result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query()", MySG(result_allocated));
     666             :                 }
     667             :         }
     668             : 
     669       20394 :         if (MySG(connect_error)!=NULL) {
     670          10 :                 efree(MySG(connect_error));
     671             :         }
     672             : 
     673             : #if defined(A0) && defined(MYSQL_USE_MYSQLND)
     674             :         zend_hash_apply(&EG(persistent_list), (apply_func_t) php_mysql_persistent_helper TSRMLS_CC);
     675             : #endif
     676             : 
     677       20394 :         return SUCCESS;
     678             : }
     679             : /* }}} */
     680             : 
     681             : /* {{{ PHP_MINFO_FUNCTION
     682             :  */
     683         143 : PHP_MINFO_FUNCTION(mysql)
     684             : {
     685             :         char buf[32];
     686             : 
     687         143 :         php_info_print_table_start();
     688         143 :         php_info_print_table_header(2, "MySQL Support", "enabled");
     689         143 :         snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MySG(num_persistent));
     690         143 :         php_info_print_table_row(2, "Active Persistent Links", buf);
     691         143 :         snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MySG(num_links));
     692         143 :         php_info_print_table_row(2, "Active Links", buf);
     693         143 :         php_info_print_table_row(2, "Client API version", mysql_get_client_info());
     694             : #if !defined (PHP_WIN32) && !defined (NETWARE) && !defined(MYSQL_USE_MYSQLND)
     695             :         php_info_print_table_row(2, "MYSQL_MODULE_TYPE", PHP_MYSQL_TYPE);
     696             :         php_info_print_table_row(2, "MYSQL_SOCKET", MYSQL_UNIX_ADDR);
     697             :         php_info_print_table_row(2, "MYSQL_INCLUDE", PHP_MYSQL_INCLUDE);
     698             :         php_info_print_table_row(2, "MYSQL_LIBS", PHP_MYSQL_LIBS);
     699             : #endif
     700             : 
     701         143 :         php_info_print_table_end();
     702             : 
     703         143 :         DISPLAY_INI_ENTRIES();
     704             : 
     705         143 : }
     706             : /* }}} */
     707             : 
     708             : /* {{{ php_mysql_do_connect
     709             :  */
     710             : #define MYSQL_DO_CONNECT_CLEANUP()      \
     711             :         if (free_host) {                                \
     712             :                 efree(host);                            \
     713             :         }
     714             : 
     715             : #define MYSQL_DO_CONNECT_RETURN_FALSE()         \
     716             :         MYSQL_DO_CONNECT_CLEANUP();                             \
     717             :         RETURN_FALSE;
     718             : 
     719             : #ifdef MYSQL_USE_MYSQLND
     720             : #define MYSQL_PORT 0
     721             : #endif
     722             : 
     723         224 : static void php_mysql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
     724             : {
     725         224 :         char *user = NULL, *passwd = NULL;
     726         224 :         char *host_and_port = NULL, *socket = NULL;
     727         224 :         char *tmp = NULL, *host = NULL;
     728         224 :         size_t user_len = 0, passwd_len = 0, host_len = 0;
     729         224 :         int port = MYSQL_PORT;
     730         224 :         zend_long client_flags = 0;
     731         224 :         php_mysql_conn *mysql = NULL;
     732             : #if MYSQL_VERSION_ID <= 32230
     733             :         void (*handler) (int);
     734             : #endif
     735             :         zend_long connect_timeout;
     736         224 :         zend_string *hashed_details = NULL;
     737         224 :         zend_bool free_host = 0, new_link = 0;
     738             : 
     739         224 :     php_error_docref(NULL TSRMLS_CC,
     740             :                      E_DEPRECATED,
     741             :                      "The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead");
     742             : 
     743             : #if !defined(MYSQL_USE_MYSQLND)
     744             :         if ((MYSQL_VERSION_ID / 100) != (mysql_get_client_version() / 100)) {
     745             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,
     746             :                                                 "Headers and client library minor version mismatch. Headers:%d Library:%ld",
     747             :                                                 MYSQL_VERSION_ID, mysql_get_client_version());
     748             :         }
     749             : #endif
     750             : 
     751         224 :         connect_timeout = MySG(connect_timeout);
     752             : 
     753         224 :         socket = MySG(default_socket);
     754             : 
     755         224 :         if (MySG(default_port) < 0) {
     756             : #if !defined(PHP_WIN32) && !defined(NETWARE)
     757             :                 struct servent *serv_ptr;
     758             :                 char *env;
     759             : 
     760         166 :                 MySG(default_port) = MYSQL_PORT;
     761         166 :                 if ((serv_ptr = getservbyname("mysql", "tcp"))) {
     762         166 :                         MySG(default_port) = (uint)ntohs((ushort)serv_ptr->s_port);
     763             :                 }
     764         166 :                 if ((env = getenv("MYSQL_TCP_PORT"))) {
     765           1 :                         MySG(default_port) = (uint)atoi(env);
     766             :                 }
     767             : #else
     768             :                 MySG(default_port) = MYSQL_PORT;
     769             : #endif
     770             :         }
     771             : 
     772         224 :         if (PG(sql_safe_mode)) {
     773           1 :                 if (ZEND_NUM_ARGS()>0) {
     774           1 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "SQL safe mode in effect - ignoring host/user/password information");
     775             :                 }
     776           1 :                 user = php_get_current_user(TSRMLS_C);
     777           2 :                 hashed_details = zend_string_alloc(sizeof("mysql___") + strlen(user) - 1, 0);
     778           1 :                 snprintf(hashed_details->val, hashed_details->len + 1, "mysql__%s_", user);
     779           1 :                 client_flags = CLIENT_INTERACTIVE;
     780             :         } else {
     781             :                 /* mysql_pconnect does not support new_link parameter */
     782         223 :                 if (persistent) {
     783          22 :                         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!s!l", &host_and_port, &host_len,
     784             :                                                                         &user, &user_len, &passwd, &passwd_len,
     785             :                                                                         &client_flags)==FAILURE) {
     786           1 :                                 return;
     787             :                 }
     788             :                 } else {
     789         201 :                         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!s!bl", &host_and_port, &host_len,
     790             :                                                                                 &user, &user_len, &passwd, &passwd_len,
     791             :                                                                                 &new_link, &client_flags)==FAILURE) {
     792           1 :                                 return;
     793             :                         }
     794             :                 }
     795             : 
     796         221 :                 if (!host_and_port) {
     797          12 :                         host_and_port = MySG(default_host);
     798             :                 }
     799         221 :                 if (!user) {
     800          14 :                         user = MySG(default_user);
     801             :                 }
     802         221 :                 if (!passwd) {
     803          16 :                         passwd = MySG(default_password);
     804          16 :                         passwd_len = passwd? strlen(passwd):0;
     805             :                 }
     806             : 
     807             :                 /* disable local infile option for open_basedir */
     808             : #if PHP_API_VERSION < 20100412
     809             :                 if (((PG(open_basedir) && PG(open_basedir)[0] != '\0') || PG(safe_mode)) && (client_flags & CLIENT_LOCAL_FILES)) {
     810             : #else
     811         221 :                 if ((PG(open_basedir) && PG(open_basedir)[0] != '\0') && (client_flags & CLIENT_LOCAL_FILES)) {
     812             : #endif
     813           1 :                         client_flags ^= CLIENT_LOCAL_FILES;
     814             :                 }
     815             : 
     816             : #ifdef CLIENT_MULTI_RESULTS
     817         221 :                 client_flags |= CLIENT_MULTI_RESULTS; /* compatibility with 5.2, see bug#50416 */
     818             : #endif
     819             : #ifdef CLIENT_MULTI_STATEMENTS
     820         221 :                 client_flags &= ~CLIENT_MULTI_STATEMENTS;   /* don't allow multi_queries via connect parameter */
     821             : #endif
     822         879 :                 hashed_details = zend_string_alloc(sizeof("mysql____") + (host_and_port? strlen(host_and_port) : 0)
     823         437 :                                 + (user? strlen(user) : 0) + (passwd? strlen(passwd) : 0) + MAX_LENGTH_OF_LONG - 1, 0);
     824         221 :                 hashed_details->len = snprintf(hashed_details->val, hashed_details->len + 1, "mysql_%s_%s_%s_" ZEND_LONG_FMT, SAFE_STRING(host_and_port), SAFE_STRING(user), SAFE_STRING(passwd), client_flags);
     825             :         }
     826             : 
     827             :         /* We cannot use mysql_port anymore in windows, need to use
     828             :          * mysql_real_connect() to set the port.
     829             :          */
     830         430 :         if (host_and_port && (tmp = strchr(host_and_port, ':'))) {
     831         208 :                 host = estrndup(host_and_port, tmp-host_and_port);
     832         208 :                 free_host = 1;
     833         208 :                 tmp++;
     834         208 :                 if (tmp[0] != '/') {
     835           3 :                         port = atoi(tmp);
     836           3 :                         if ((tmp=strchr(tmp, ':'))) {
     837           0 :                                 tmp++;
     838           0 :                                 socket=tmp;
     839             :                         }
     840             :                 } else {
     841         205 :                         socket = tmp;
     842             :                 }
     843             :         } else {
     844          14 :                 host = host_and_port;
     845          14 :                 port = MySG(default_port);
     846             :         }
     847             : 
     848             : #if MYSQL_VERSION_ID < 32200
     849             :         mysql_port = port;
     850             : #endif
     851             : 
     852         222 :         if (!MySG(allow_persistent)) {
     853           5 :                 persistent = 0;
     854             :         }
     855             : 
     856         222 :         if (persistent) {
     857             :                 zend_resource *le;
     858             :                 zend_string *phashed;
     859             : 
     860             :                 /* try to find if we already have this link in our persistent list */
     861          19 :                 if ((le = zend_hash_find_ptr(&EG(persistent_list), hashed_details)) == NULL) { /* we don't */
     862             :                         zval new_le;
     863             : 
     864          12 :                         if (MySG(max_links) != -1 && MySG(num_links) >= MySG(max_links)) {
     865           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%pd)", MySG(num_links));
     866             :                                 zend_string_release(hashed_details);
     867           1 :                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     868             :                         }
     869             : 
     870          11 :                         if (MySG(max_persistent) != -1 && MySG(num_persistent) >= MySG(max_persistent)) {
     871           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open persistent links (%pd)", MySG(num_persistent));
     872             :                                 zend_string_release(hashed_details);
     873           1 :                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     874             :                         }
     875             :                         /* create the link */
     876          10 :                         mysql = (php_mysql_conn *)malloc(sizeof(php_mysql_conn));
     877          10 :                         if (!mysql) {
     878           0 :                                 php_error_docref(NULL TSRMLS_CC, E_ERROR,
     879             :                                                 "Out of memory while allocating memory for a persistent link");
     880             :                         }
     881          10 :                         mysql->active_result_res = NULL;
     882             : #ifdef CLIENT_MULTI_STATEMENTS
     883          10 :                         mysql->multi_query = client_flags & CLIENT_MULTI_STATEMENTS? 1:0;
     884             : #else
     885             :                         mysql->multi_query = 0;
     886             : #endif
     887             : 
     888             : #ifndef MYSQL_USE_MYSQLND
     889             :                         mysql->conn = mysql_init(NULL);
     890             : #else
     891          10 :                         mysql->conn = mysqlnd_init(MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA, persistent);
     892             : #endif
     893             : 
     894          10 :                         if (connect_timeout != -1) {
     895          10 :                                 mysql_options(mysql->conn, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout);
     896             :                         }
     897             : #ifndef MYSQL_USE_MYSQLND
     898             :                         if (mysql_real_connect(mysql->conn, host, user, passwd, NULL, port, socket, client_flags)==NULL)
     899             : #else
     900          10 :                         if (mysqlnd_connect(mysql->conn, host, user, passwd, passwd_len, NULL, 0, port, socket, client_flags, MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA TSRMLS_CC) == NULL)
     901             : #endif
     902             :                         {
     903             :                                 /* Populate connect error globals so that the error functions can read them */
     904           1 :                                 if (MySG(connect_error) != NULL) {
     905           0 :                                         efree(MySG(connect_error));
     906             :                                 }
     907             : 
     908           1 :                                 MySG(connect_error) = estrdup(mysql_error(mysql->conn));
     909           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", MySG(connect_error));
     910             : #if defined(HAVE_MYSQL_ERRNO)
     911           1 :                                 MySG(connect_errno) = mysql_errno(mysql->conn);
     912             : #endif
     913           1 :                                 free(mysql);
     914             :                                 zend_string_release(hashed_details);
     915           1 :                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     916             :                         }
     917           9 :                         mysql_options(mysql->conn, MYSQL_OPT_LOCAL_INFILE, (char *)&MySG(allow_local_infile));
     918             : 
     919             :                         /* hash it up */
     920           9 :                         ZVAL_NEW_PERSISTENT_RES(&new_le, -1, mysql, le_plink);
     921             :                         
     922             :                         /* avoid bogus memleak report */
     923          18 :                         phashed = zend_string_init(hashed_details->val, hashed_details->len, 1);
     924           9 :                         if (zend_hash_update(&EG(persistent_list), phashed, &new_le) == NULL) {
     925             :                                 zend_string_release(phashed);
     926           0 :                                 free(mysql);
     927             :                                 zend_string_release(hashed_details);
     928           0 :                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     929             :                         }
     930             :                         zend_string_release(phashed);
     931           9 :                         MySG(num_persistent)++;
     932           9 :                         MySG(num_links)++;
     933             :                 } else {  /* The link is in our list of persistent connections */
     934           7 :                         if (le->type != le_plink) {
     935           0 :                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     936             :                         }
     937           7 :                         mysql = (php_mysql_conn *)le->ptr;
     938           7 :                         mysql->active_result_res = NULL;
     939             : #ifdef CLIENT_MULTI_STATEMENTS
     940           7 :                         mysql->multi_query = client_flags & CLIENT_MULTI_STATEMENTS? 1:0;
     941             : #else
     942             :                         mysql->multi_query = 0;
     943             : #endif
     944             :                         /* ensure that the link did not die */
     945             : #if defined(A0) && MYSQL_USE_MYSQLND
     946             :                         mysqlnd_end_psession(mysql->conn);
     947             : #endif
     948           7 :                         if (mysql_ping(mysql->conn)) {
     949           1 :                                 if (mysql_errno(mysql->conn) == 2006) {
     950             : #ifndef MYSQL_USE_MYSQLND
     951             :                                         if (mysql_real_connect(mysql->conn, host, user, passwd, NULL, port, socket, client_flags)==NULL)
     952             : #else
     953           1 :                                         if (mysqlnd_connect(mysql->conn, host, user, passwd, passwd_len, NULL, 0, port, socket, client_flags, MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA TSRMLS_CC) == NULL)
     954             : #endif
     955             :                                         {
     956           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Link to server lost, unable to reconnect");
     957           0 :                                                 zend_hash_del(&EG(persistent_list), hashed_details);
     958             :                                                 zend_string_release(hashed_details);
     959           0 :                                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     960             :                                         }
     961           1 :                                         mysql_options(mysql->conn, MYSQL_OPT_LOCAL_INFILE, (char *)&MySG(allow_local_infile));
     962             :                                 }
     963             :                         } else {
     964             : #ifdef MYSQL_USE_MYSQLND
     965           6 :                                 mysqlnd_restart_psession(mysql->conn);
     966             : #endif
     967             :                         }
     968             :                 }
     969          16 :                 ZEND_REGISTER_RESOURCE(return_value, mysql, le_plink);
     970             :         } else { /* non persistent */
     971             :                 zend_resource *index_ptr;
     972             :                 zval new_index_ptr;
     973             : 
     974             :                 /* first we check the hash for the hashed_details key.  if it exists,
     975             :                  * it should point us to the right offset where the actual mysql link sits.
     976             :                  * if it doesn't, open a new mysql link, add it to the resource list,
     977             :                  * and add a pointer to it with hashed_details as the key.
     978             :                  */
     979         221 :                 if (!new_link && (index_ptr = zend_hash_find_ptr(&EG(regular_list), hashed_details))) {
     980             :                         zend_resource *link;
     981             : 
     982           8 :                         if (index_ptr->type != le_index_ptr) {
     983           0 :                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     984             :                         }
     985             : 
     986           8 :                         link = (zend_resource *)index_ptr->ptr;
     987           8 :                         if (link && (link->type == le_link || link->type == le_plink)) {
     988           5 :                                 GC_REFCOUNT(link)++;
     989           5 :                                 ZVAL_RES(return_value, link);
     990           5 :                                 php_mysql_set_default_link(link TSRMLS_CC);
     991             :                                 zend_string_release(hashed_details);
     992           5 :                                 MYSQL_DO_CONNECT_CLEANUP();
     993           5 :                                 return;
     994             :                         } else {
     995           3 :                                 zend_hash_del(&EG(regular_list), hashed_details);
     996             :                         }
     997             :                 }
     998             : 
     999         198 :                 if (MySG(max_links) != -1 && MySG(num_links) >= MySG(max_links)) {
    1000           3 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%pd)", MySG(num_links));
    1001             :                         zend_string_release(hashed_details);
    1002           3 :                         MYSQL_DO_CONNECT_RETURN_FALSE();
    1003             :                 }
    1004             : 
    1005         195 :                 mysql = (php_mysql_conn *) emalloc(sizeof(php_mysql_conn));
    1006         195 :                 mysql->active_result_res = NULL;
    1007             : #ifdef CLIENT_MULTI_STATEMENTS
    1008         195 :                 mysql->multi_query = 1;
    1009             : #endif
    1010             : 
    1011             : #ifndef MYSQL_USE_MYSQLND
    1012             :                 mysql->conn = mysql_init(NULL);
    1013             : #else
    1014         195 :                 mysql->conn = mysqlnd_init(MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA, persistent);
    1015             : #endif
    1016         195 :                 if (!mysql->conn) {
    1017           0 :                         MySG(connect_error) = estrdup("OOM");
    1018           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "OOM");
    1019             :                         zend_string_release(hashed_details);
    1020           0 :                         efree(mysql);
    1021           0 :                         MYSQL_DO_CONNECT_RETURN_FALSE();
    1022             :                 }
    1023             : 
    1024         195 :                 if (connect_timeout != -1) {
    1025         195 :                         mysql_options(mysql->conn, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout);
    1026             :                 }
    1027             : 
    1028             : #ifndef MYSQL_USE_MYSQLND
    1029             :                 if (mysql_real_connect(mysql->conn, host, user, passwd, NULL, port, socket, client_flags)==NULL)
    1030             : #else
    1031         195 :                 if (mysqlnd_connect(mysql->conn, host, user, passwd, passwd_len, NULL, 0, port, socket, client_flags, MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA TSRMLS_CC) == NULL)
    1032             : #endif
    1033             :                 {
    1034             :                         /* Populate connect error globals so that the error functions can read them */
    1035           9 :                         if (MySG(connect_error) != NULL) {
    1036           0 :                                 efree(MySG(connect_error));
    1037             :                         }
    1038           9 :                         MySG(connect_error) = estrdup(mysql_error(mysql->conn));
    1039           9 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", MySG(connect_error));
    1040             : #if defined(HAVE_MYSQL_ERRNO)
    1041           9 :                         MySG(connect_errno) = mysql_errno(mysql->conn);
    1042             : #endif
    1043             :                         /* free mysql structure */
    1044             : #ifdef MYSQL_USE_MYSQLND
    1045           9 :                         mysqlnd_close(mysql->conn, MYSQLND_CLOSE_DISCONNECTED);
    1046             : #endif
    1047             :                         zend_string_release(hashed_details);
    1048           9 :                         efree(mysql);
    1049           9 :                         MYSQL_DO_CONNECT_RETURN_FALSE();
    1050             :                 }
    1051         186 :                 mysql_options(mysql->conn, MYSQL_OPT_LOCAL_INFILE, (char *)&MySG(allow_local_infile));
    1052             : 
    1053             :                 /* add it to the list */
    1054         186 :                 ZEND_REGISTER_RESOURCE(return_value, mysql, le_link);
    1055             : 
    1056             :                 /* add it to the hash */
    1057         186 :                 ZVAL_NEW_RES(&new_index_ptr, -1, Z_RES_P(return_value), le_index_ptr);              
    1058         186 :                 if (zend_hash_update(&EG(regular_list), hashed_details, &new_index_ptr) == NULL) {
    1059           0 :                         zval_ptr_dtor(return_value);
    1060             :                         zend_string_release(hashed_details);
    1061           0 :                         MYSQL_DO_CONNECT_RETURN_FALSE();
    1062             :                 }
    1063             :                 Z_ADDREF_P(return_value);
    1064         186 :                 MySG(num_links)++;
    1065             :         }
    1066             : 
    1067             :         zend_string_release(hashed_details);
    1068         202 :         php_mysql_set_default_link(Z_RES_P(return_value) TSRMLS_CC);
    1069         202 :         MYSQL_DO_CONNECT_CLEANUP();
    1070             : }
    1071             : /* }}} */
    1072             : 
    1073             : /* {{{ php_mysql_get_default_link
    1074             :  */
    1075          32 : static zend_resource *php_mysql_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
    1076             : {
    1077          32 :         if (MySG(default_link) == NULL) { /* no link opened yet, implicitly open one */
    1078           7 :                 ZEND_NUM_ARGS() = 0;
    1079           7 :                 php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
    1080             :         }
    1081          32 :         return MySG(default_link);
    1082             : }
    1083             : /* }}} */
    1084             : 
    1085             : /* {{{ proto resource mysql_connect([string hostname[:port][:/path/to/socket] [, string username [, string password [, bool new [, int flags]]]]])
    1086             :    Opens a connection to a MySQL Server */
    1087         195 : PHP_FUNCTION(mysql_connect)
    1088             : {
    1089         195 :         php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
    1090         195 : }
    1091             : /* }}} */
    1092             : 
    1093             : /* {{{ proto resource mysql_pconnect([string hostname[:port][:/path/to/socket] [, string username [, string password [, int flags]]]])
    1094             :    Opens a persistent connection to a MySQL Server */
    1095          22 : PHP_FUNCTION(mysql_pconnect)
    1096             : {
    1097          22 :         php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    1098          22 : }
    1099             : /* }}} */
    1100             : 
    1101             : /* {{{ proto bool mysql_close([int link_identifier])
    1102             :    Close a MySQL connection */
    1103         192 : PHP_FUNCTION(mysql_close)
    1104             : {
    1105             :         zend_resource *res;
    1106         192 :         zval *mysql_link = NULL;
    1107             :         php_mysql_conn *mysql;
    1108             : 
    1109         192 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1110           2 :                 return;
    1111             :         }
    1112             : 
    1113         190 :         if (mysql_link) {
    1114         183 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1115             :         } else {
    1116           7 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, NULL, MySG(default_link)? MySG(default_link)->handle : -1, "MySQL-Link", le_link, le_plink);
    1117             :         }
    1118             : 
    1119         187 :         res = mysql_link ? Z_RES_P(mysql_link) : MySG(default_link);
    1120         187 :         PHPMY_UNBUFFERED_QUERY_CHECK();
    1121         187 :         if (res) {
    1122             : #ifdef MYSQL_USE_MYSQLND
    1123         187 :                 if (res->type == le_plink) {
    1124          15 :                         mysqlnd_end_psession(mysql->conn);
    1125             :                 }
    1126             : #endif
    1127         187 :                 if (res == MySG(default_link)) {
    1128         180 :                         zend_list_delete(res);
    1129         180 :                         MySG(default_link) = NULL;
    1130             :                 }
    1131         187 :                 if (mysql_link) {
    1132             :                         /* we have at least 3 additional references to this resource ??? */
    1133         183 :                         if (GC_REFCOUNT(res) <= 3) {
    1134         179 :                                 zend_list_close(res);
    1135             :                         }
    1136             :                 }
    1137             :         }
    1138             : 
    1139         187 :         RETURN_TRUE;
    1140             : }
    1141             : /* }}} */
    1142             : 
    1143             : /* {{{ proto bool mysql_select_db(string database_name [, int link_identifier])
    1144             :    Selects a MySQL database */
    1145         186 : PHP_FUNCTION(mysql_select_db)
    1146             : {
    1147             :         char *db;
    1148             :         size_t db_len;
    1149             :         php_mysql_conn *mysql;
    1150         186 :         zval *mysql_link = NULL;
    1151             : 
    1152         186 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &db, &db_len, &mysql_link) == FAILURE) {
    1153           1 :                 return;
    1154             :         }
    1155             : 
    1156         185 :         if (!mysql_link) {
    1157           1 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1158           1 :                 CHECK_LINK(res);
    1159           0 :                 mysql = (php_mysql_conn*)res->ptr;
    1160             :         } else {
    1161         184 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1162             :         }
    1163             : 
    1164         183 :         if (php_mysql_select_db(mysql, db TSRMLS_CC)) {
    1165         181 :                 RETURN_TRUE;
    1166             :         } else {
    1167           2 :                 RETURN_FALSE;
    1168             :         }
    1169             : }
    1170             : /* }}} */
    1171             : 
    1172             : #ifdef HAVE_GETINFO_FUNCS
    1173             : 
    1174             : /* {{{ proto string mysql_get_client_info(void)
    1175             :    Returns a string that represents the client library version */
    1176         179 : PHP_FUNCTION(mysql_get_client_info)
    1177             : {
    1178         179 :         if (zend_parse_parameters_none() == FAILURE) {
    1179           1 :                 return;
    1180             :         }
    1181             : 
    1182         356 :         RETURN_STRING((char *)mysql_get_client_info());
    1183             : }
    1184             : /* }}} */
    1185             : 
    1186             : /* {{{ proto string mysql_get_host_info([int link_identifier])
    1187             :    Returns a string describing the type of connection in use, including the server host name */
    1188           4 : PHP_FUNCTION(mysql_get_host_info)
    1189             : {
    1190           4 :         zval *mysql_link = NULL;
    1191             :         php_mysql_conn *mysql;
    1192             : 
    1193           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1194           2 :                 return;
    1195             :         }
    1196             : 
    1197           2 :         if (!mysql_link) {
    1198           1 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1199           1 :                 CHECK_LINK(res);
    1200           1 :                 mysql = (php_mysql_conn*)res->ptr;
    1201             :         } else {
    1202           1 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1203             :         }
    1204             : 
    1205           4 :         RETURN_STRING((char *)mysql_get_host_info(mysql->conn));
    1206             : }
    1207             : /* }}} */
    1208             : 
    1209             : /* {{{ proto int mysql_get_proto_info([int link_identifier])
    1210             :    Returns the protocol version used by current connection */
    1211           4 : PHP_FUNCTION(mysql_get_proto_info)
    1212             : {
    1213           4 :         zval *mysql_link = NULL;
    1214             :         php_mysql_conn *mysql;
    1215             : 
    1216           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1217           2 :                 return;
    1218             :         }
    1219             : 
    1220           2 :         if (!mysql_link) {
    1221           1 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1222           1 :                 CHECK_LINK(res);
    1223           1 :                 mysql = (php_mysql_conn*)res->ptr;
    1224             :         } else {
    1225           1 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1226             :         }
    1227             : 
    1228           2 :         RETURN_LONG(mysql_get_proto_info(mysql->conn));
    1229             : }
    1230             : /* }}} */
    1231             : 
    1232             : /* {{{ proto string mysql_get_server_info([int link_identifier])
    1233             :    Returns a string that represents the server version number */
    1234           7 : PHP_FUNCTION(mysql_get_server_info)
    1235             : {
    1236           7 :         zval *mysql_link = NULL;
    1237             :         php_mysql_conn *mysql;
    1238             : 
    1239           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1240           2 :                 return;
    1241             :         }
    1242             : 
    1243           5 :         if (!mysql_link) {
    1244           1 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1245           1 :                 CHECK_LINK(res);
    1246           1 :                 mysql = (php_mysql_conn*)res->ptr;
    1247             :         } else {
    1248           4 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1249             :         }
    1250             : 
    1251          10 :         RETURN_STRING((char *)mysql_get_server_info(mysql->conn));
    1252             : }
    1253             : /* }}} */
    1254             : 
    1255             : /* {{{ proto string mysql_info([int link_identifier])
    1256             :    Returns a string containing information about the most recent query */
    1257           8 : PHP_FUNCTION(mysql_info)
    1258             : {
    1259             :         char *str;
    1260           8 :         zval *mysql_link = NULL;
    1261             :         php_mysql_conn *mysql;
    1262             : 
    1263           8 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1264           1 :                 return;
    1265             :         }
    1266             :         
    1267           7 :         if (!mysql_link) {
    1268           2 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1269           2 :                 CHECK_LINK(res);
    1270           1 :                 mysql = (php_mysql_conn*)res->ptr;
    1271             :         } else {
    1272           5 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1273             :         }
    1274             : 
    1275           6 :         if ((str = (char *)mysql_info(mysql->conn))) {
    1276          10 :                 RETURN_STRING(str);
    1277             :         } else {
    1278           1 :                 RETURN_FALSE;
    1279             :         }
    1280             : }
    1281             : /* }}} */
    1282             : 
    1283             : /* {{{ proto int mysql_thread_id([int link_identifier])
    1284             :         Returns the thread id of current connection */
    1285          19 : PHP_FUNCTION(mysql_thread_id)
    1286             : {
    1287          19 :         zval *mysql_link = NULL;
    1288             :         php_mysql_conn *mysql;
    1289             : 
    1290          19 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1291           1 :                 return;
    1292             :         }
    1293             : 
    1294          18 :         if (!mysql_link) {
    1295           3 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1296           3 :                 CHECK_LINK(res);
    1297           3 :                 mysql = (php_mysql_conn*)res->ptr;
    1298             :         } else {
    1299          15 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1300             :         }
    1301             : 
    1302          17 :         RETURN_LONG((zend_long) mysql_thread_id(mysql->conn));
    1303             : }
    1304             : /* }}} */
    1305             : 
    1306             : /* {{{ proto string mysql_stat([int link_identifier])
    1307             :         Returns a string containing status information */
    1308           5 : PHP_FUNCTION(mysql_stat)
    1309             : {
    1310             :         php_mysql_conn *mysql;
    1311           5 :         zval *mysql_link = NULL;
    1312             : #ifndef MYSQL_USE_MYSQLND
    1313             :         char *stat;
    1314             : #else
    1315             :         zend_string *stat;
    1316             : #endif
    1317             : 
    1318             : 
    1319           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1320           2 :                 return;
    1321             :         }
    1322             : 
    1323           3 :         if (!mysql_link) {
    1324           1 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1325           1 :                 CHECK_LINK(res);
    1326           1 :                 mysql = (php_mysql_conn*)res->ptr;
    1327             :         } else {
    1328           2 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1329             :         }
    1330             : 
    1331           2 :         PHPMY_UNBUFFERED_QUERY_CHECK();
    1332             : #ifndef MYSQL_USE_MYSQLND
    1333             :         if ((stat = (char *)mysql_stat(mysql->conn))) {
    1334             :                 RETURN_STRING(stat);
    1335             : #else
    1336           2 :         if (mysqlnd_stat(mysql->conn, &stat) == PASS) {
    1337           2 :                 RETURN_STR(stat);
    1338             : #endif
    1339             :         } else {
    1340           0 :                 RETURN_FALSE;
    1341             :         }
    1342             : }
    1343             : /* }}} */
    1344             : 
    1345             : /* {{{ proto string mysql_client_encoding([int link_identifier])
    1346             :         Returns the default character set for the current connection */
    1347           3 : PHP_FUNCTION(mysql_client_encoding)
    1348             : {
    1349             :         php_mysql_conn *mysql;
    1350           3 :         zval *mysql_link = NULL;
    1351             : 
    1352           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1353           0 :                 return;
    1354             :         }
    1355             : 
    1356           3 :         if (!mysql_link) {
    1357           1 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1358           1 :                 CHECK_LINK(res);
    1359           1 :                 mysql = (php_mysql_conn*)res->ptr;
    1360             :         } else {
    1361           2 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1362             :         }
    1363             : 
    1364           4 :         RETURN_STRING((char *)mysql_character_set_name(mysql->conn));
    1365             : }
    1366             : /* }}} */
    1367             : #endif
    1368             : 
    1369             : #ifdef MYSQL_HAS_SET_CHARSET
    1370             : /* {{{ proto bool mysql_set_charset(string csname [, int link_identifier])
    1371             :    sets client character set */
    1372           0 : PHP_FUNCTION(mysql_set_charset)
    1373             : {
    1374             :         char *csname;
    1375             :         size_t csname_len;
    1376             :         php_mysql_conn *mysql;
    1377           0 :         zval *mysql_link = NULL;
    1378             : 
    1379           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &csname, &csname_len, &mysql_link) == FAILURE) {
    1380           0 :                 return;
    1381             :         }
    1382             : 
    1383           0 :         if (!mysql_link) {
    1384           0 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1385           0 :                 CHECK_LINK(res);
    1386           0 :                 mysql = (php_mysql_conn*)res->ptr;
    1387             :         } else {
    1388           0 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1389             :         }
    1390             : 
    1391           0 :         if (!mysql_set_character_set(mysql->conn, csname)) {
    1392           0 :                 RETURN_TRUE;
    1393             :         } else {
    1394           0 :                 RETURN_FALSE;
    1395             :         }
    1396             : }
    1397             : /* }}} */
    1398             : #endif
    1399             : 
    1400             : #ifndef NETWARE         /* The below two functions not supported on NetWare */
    1401             : #if MYSQL_VERSION_ID < 40000
    1402             : /* {{{ proto bool mysql_create_db(string database_name [, int link_identifier])
    1403             :    Create a MySQL database */
    1404             : PHP_FUNCTION(mysql_create_db)
    1405             : {
    1406             :         char *db;
    1407             :         size_t db_len;
    1408             :         php_mysql_conn *mysql;
    1409             :         zval *mysql_link = NULL;
    1410             : 
    1411             :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &db, &db_len, &mysql_link) == FAILURE) {
    1412             :                 return;
    1413             :         }
    1414             : 
    1415             :         if (!mysql_link) {
    1416             :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1417             :                 CHECK_LINK(res);
    1418             :                 mysql = (php_mysql_conn*)res->ptr;
    1419             :         } else {
    1420             :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1421             :         }
    1422             : 
    1423             :         PHPMY_UNBUFFERED_QUERY_CHECK();
    1424             : 
    1425             :         if (mysql_create_db(mysql->conn, db)==0) {
    1426             :                 RETURN_TRUE;
    1427             :         } else {
    1428             :                 RETURN_FALSE;
    1429             :         }
    1430             : }
    1431             : /* }}} */
    1432             : 
    1433             : /* {{{ proto bool mysql_drop_db(string database_name [, int link_identifier])
    1434             :    Drops (delete) a MySQL database */
    1435             : PHP_FUNCTION(mysql_drop_db)
    1436             : {
    1437             :         char *db;
    1438             :         size_t db_len;
    1439             :         php_mysql_conn *mysql;
    1440             :         zval *mysql_link = NULL;
    1441             : 
    1442             :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &db, &db_len, &mysql_link) == FAILURE) {
    1443             :                 return;
    1444             :         }
    1445             : 
    1446             :         if (!mysql_link) {
    1447             :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1448             :                 CHECK_LINK(res);
    1449             :                 mysql = (php_mysql_conn*)res->ptr;
    1450             :         } else {
    1451             :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1452             :         }
    1453             : 
    1454             :         if (mysql_drop_db(mysql->conn, db)==0) {
    1455             :                 RETURN_TRUE;
    1456             :         } else {
    1457             :                 RETURN_FALSE;
    1458             :         }
    1459             : }
    1460             : /* }}} */
    1461             : #endif
    1462             : #endif  /* NETWARE */
    1463             : 
    1464             : /* {{{ php_mysql_do_query_general
    1465             :  */
    1466         829 : static void php_mysql_do_query_general(php_mysql_conn *mysql, char *query, int query_len, char *db, int use_store, zval *return_value TSRMLS_DC)
    1467             : {
    1468             :         MYSQL_RES *mysql_result;
    1469             : 
    1470         829 :         if (db) {
    1471           4 :                 if (!php_mysql_select_db(mysql, db TSRMLS_CC)) {
    1472           0 :                         RETURN_FALSE;
    1473             :                 }
    1474             :         }
    1475             : 
    1476         829 :         PHPMY_UNBUFFERED_QUERY_CHECK();
    1477             : 
    1478         829 :         MYSQL_DISABLE_MQ;
    1479             : 
    1480             : #ifndef MYSQL_USE_MYSQLND
    1481             :         /* check explain */
    1482             :         if (MySG(trace_mode)) {
    1483             :                 if (!strncasecmp("select", query, 6)){
    1484             :                         MYSQL_ROW       row;
    1485             : 
    1486             :                         char *newquery;
    1487             :                         int newql = spprintf(&newquery, 0, "EXPLAIN %s", query);
    1488             :                         mysql_real_query(mysql->conn, newquery, newql);
    1489             :                         efree (newquery);
    1490             :                         if (mysql_errno(mysql->conn)) {
    1491             :                                 php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "%s", mysql_error(mysql->conn));
    1492             :                                 RETURN_FALSE;
    1493             :                         }
    1494             :                         else {
    1495             :                         mysql_result = mysql_use_result(mysql->conn);
    1496             :                                 while ((row = mysql_fetch_row(mysql_result))) {
    1497             :                                         if (!strcmp("ALL", row[1])) {
    1498             :                                                 php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "Your query requires a full tablescan (table %s, %s rows affected). Use EXPLAIN to optimize your query.", row[0], row[6]);
    1499             :                                         } else if (!strcmp("INDEX", row[1])) {
    1500             :                                                 php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "Your query requires a full indexscan (table %s, %s rows affected). Use EXPLAIN to optimize your query.", row[0], row[6]);
    1501             :                                         }
    1502             :                                 }
    1503             :                                 mysql_free_result(mysql_result);
    1504             :                         }
    1505             :                 }
    1506             :         } /* end explain */
    1507             : #endif
    1508             : 
    1509             :         /* mysql_query is binary unsafe, use mysql_real_query */
    1510             : #if MYSQL_VERSION_ID > 32199
    1511         829 :         if (mysql_real_query(mysql->conn, query, query_len)!=0) {
    1512             :                 /* check possible error */
    1513          20 :                 if (MySG(trace_mode)){
    1514           1 :                         if (mysql_errno(mysql->conn)){
    1515           1 :                                 php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "%s", mysql_error(mysql->conn));
    1516             :                         }
    1517             :                 }
    1518          20 :                 RETURN_FALSE;
    1519             :         }
    1520             : #else
    1521             :         if (mysql_query(mysql->conn, query)!=0) {
    1522             :                 /* check possible error */
    1523             :                 if (MySG(trace_mode)){
    1524             :                         if (mysql_errno(mysql->conn)){
    1525             :                                 php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "%s", mysql_error(mysql->conn));
    1526             :                         }
    1527             :                 }
    1528             :                 RETURN_FALSE;
    1529             :         }
    1530             : #endif
    1531         809 :         if(use_store == MYSQL_USE_RESULT) {
    1532          12 :                 mysql_result = mysql_use_result(mysql->conn);
    1533             :         } else {
    1534         797 :                 mysql_result = mysql_store_result(mysql->conn);
    1535             :         }
    1536         809 :         if (!mysql_result) {
    1537         614 :                 if (PHP_MYSQL_VALID_RESULT(mysql->conn)) { /* query should have returned rows */
    1538           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save result set");
    1539           0 :                         RETURN_FALSE;
    1540             :                 } else {
    1541         614 :                         RETURN_TRUE;
    1542             :                 }
    1543             :         }
    1544         195 :         MySG(result_allocated)++;
    1545         195 :         ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
    1546         195 :         if (use_store == MYSQL_USE_RESULT) {
    1547           6 :                 mysql->active_result_res = Z_RES_P(return_value);
    1548             :                 Z_ADDREF_P(return_value);
    1549             :         }
    1550             : }
    1551             : /* }}} */
    1552             : 
    1553             : /* {{{ php_mysql_do_query
    1554             :  */
    1555         834 : static void php_mysql_do_query(INTERNAL_FUNCTION_PARAMETERS, int use_store)
    1556             : {
    1557             :         char *query;
    1558             :         size_t query_len;
    1559         834 :         zval *mysql_link = NULL;
    1560             :         php_mysql_conn *mysql;
    1561             : 
    1562         834 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &query, &query_len, &mysql_link) == FAILURE) {
    1563           4 :                 return;
    1564             :         }
    1565             : 
    1566         830 :         if (!mysql_link) {
    1567           9 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1568           9 :                 CHECK_LINK(res);
    1569           7 :                 mysql = (php_mysql_conn*)res->ptr;
    1570             :         } else {
    1571         821 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1572             :         }
    1573             : 
    1574         825 :         php_mysql_do_query_general(mysql, query, query_len, NULL, use_store, return_value TSRMLS_CC);
    1575             : }
    1576             : /* }}} */
    1577             : 
    1578             : /* {{{ proto resource mysql_query(string query [, int link_identifier])
    1579             :    Sends an SQL query to MySQL */
    1580         816 : PHP_FUNCTION(mysql_query)
    1581             : {
    1582         816 :         php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_STORE_RESULT);
    1583         816 : }
    1584             : /* }}} */
    1585             : 
    1586             : /* {{{ proto resource mysql_unbuffered_query(string query [, int link_identifier])
    1587             :    Sends an SQL query to MySQL, without fetching and buffering the result rows */
    1588          18 : PHP_FUNCTION(mysql_unbuffered_query)
    1589             : {
    1590          18 :         php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_USE_RESULT);
    1591          18 : }
    1592             : /* }}} */
    1593             : 
    1594             : /* {{{ proto resource mysql_db_query(string database_name, string query [, int link_identifier])
    1595             :    Sends an SQL query to MySQL */
    1596           7 : PHP_FUNCTION(mysql_db_query)
    1597             : {
    1598             :         char *db, *query;
    1599             :         size_t db_len, query_len;
    1600             :         php_mysql_conn *mysql;
    1601           7 :         zval *mysql_link = NULL;
    1602             : 
    1603           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|r", &db, &db_len, &query, &query_len, &mysql_link) == FAILURE) {
    1604           3 :                 return;
    1605             :         }
    1606             : 
    1607           4 :         if (!mysql_link) {
    1608           1 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1609           1 :                 CHECK_LINK(res);
    1610           1 :                 mysql = (php_mysql_conn*)res->ptr;
    1611             :         } else {
    1612           3 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1613             :         }
    1614             : 
    1615           4 :         php_error_docref(NULL TSRMLS_CC, E_DEPRECATED, "This function is deprecated; use mysql_query() instead");
    1616             : 
    1617           4 :         php_mysql_do_query_general(mysql, query, query_len, db, MYSQL_STORE_RESULT, return_value TSRMLS_CC);
    1618             : }
    1619             : /* }}} */
    1620             : 
    1621             : /* {{{ proto resource mysql_list_dbs([int link_identifier])
    1622             :    List databases available on a MySQL server */
    1623           6 : PHP_FUNCTION(mysql_list_dbs)
    1624             : {
    1625             :         php_mysql_conn *mysql;
    1626             :         MYSQL_RES *mysql_result;
    1627           6 :         zval *mysql_link = NULL;
    1628             : 
    1629           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1630           2 :                 return;
    1631             :         }
    1632             : 
    1633           4 :         if (!mysql_link) {
    1634           1 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1635           1 :                 CHECK_LINK(res);
    1636           1 :                 mysql = (php_mysql_conn*)res->ptr;
    1637             :         } else {
    1638           3 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1639             :         }
    1640             : 
    1641           4 :         php_error_docref(NULL TSRMLS_CC, E_DEPRECATED, "This function is deprecated; use mysql_query() with SHOW DATABASES instead");
    1642             : 
    1643           4 :         PHPMY_UNBUFFERED_QUERY_CHECK();
    1644             : 
    1645           4 :         if ((mysql_result = mysql_list_dbs(mysql->conn, NULL))==NULL) {
    1646           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save MySQL query result");
    1647           0 :                 RETURN_FALSE;
    1648             :         }
    1649             : 
    1650           4 :         MySG(result_allocated)++;
    1651           4 :         ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
    1652             : }
    1653             : /* }}} */
    1654             : 
    1655             : /* {{{ proto resource mysql_list_tables(string database_name [, int link_identifier])
    1656             :    List tables in a MySQL database */
    1657           6 : PHP_FUNCTION(mysql_list_tables)
    1658             : {
    1659             :         char *db;
    1660             :         size_t db_len;
    1661             :         php_mysql_conn *mysql;
    1662           6 :         zval *mysql_link = NULL;
    1663             :         MYSQL_RES *mysql_result;
    1664             : 
    1665           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &db, &db_len, &mysql_link) == FAILURE) {
    1666           3 :                 return;
    1667             :         }
    1668             : 
    1669           3 :         if (!mysql_link) {
    1670           2 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1671           2 :                 CHECK_LINK(res);
    1672           1 :                 mysql = (php_mysql_conn*)res->ptr;
    1673             :         } else {
    1674           1 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1675             :         }
    1676             : 
    1677           2 :         if (!php_mysql_select_db(mysql, db TSRMLS_CC)) {
    1678           0 :                 RETURN_FALSE;
    1679             :         }
    1680             : 
    1681           2 :         PHPMY_UNBUFFERED_QUERY_CHECK();
    1682             : 
    1683           2 :         if ((mysql_result = mysql_list_tables(mysql->conn, NULL))==NULL) {
    1684           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save MySQL query result");
    1685           0 :                 RETURN_FALSE;
    1686             :         }
    1687           2 :         MySG(result_allocated)++;
    1688           2 :         ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
    1689             : }
    1690             : /* }}} */
    1691             : 
    1692             : /* {{{ proto resource mysql_list_fields(string database_name, string table_name [, int link_identifier])
    1693             :    List MySQL result fields */
    1694           4 : PHP_FUNCTION(mysql_list_fields)
    1695             : {
    1696             :         char *db, *table;
    1697             :         size_t db_len, table_len;
    1698           4 :         zval *mysql_link = NULL;
    1699             :         php_mysql_conn *mysql;
    1700             :         MYSQL_RES *mysql_result;
    1701             : 
    1702           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|r", &db, &db_len, &table, &table_len, &mysql_link) == FAILURE) {
    1703           0 :                 return;
    1704             :         }
    1705             : 
    1706           4 :         if (!mysql_link) {
    1707           1 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1708           1 :                 CHECK_LINK(res);
    1709           1 :                 mysql = (php_mysql_conn*)res->ptr;
    1710             :         } else {
    1711           3 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1712             :         }
    1713             : 
    1714           4 :         if (!php_mysql_select_db(mysql, db TSRMLS_CC)) {
    1715           0 :                 RETURN_FALSE;
    1716             :         }
    1717             : 
    1718           4 :         PHPMY_UNBUFFERED_QUERY_CHECK();
    1719             : 
    1720           4 :         if ((mysql_result=mysql_list_fields(mysql->conn, table, NULL))==NULL) {
    1721           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save MySQL query result");
    1722           1 :                 RETURN_FALSE;
    1723             :         }
    1724           3 :         MySG(result_allocated)++;
    1725           3 :         ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
    1726             : }
    1727             : /* }}} */
    1728             : 
    1729             : /* {{{ proto resource mysql_list_processes([int link_identifier])
    1730             :         Returns a result set describing the current server threads */
    1731           3 : PHP_FUNCTION(mysql_list_processes)
    1732             : {
    1733             :         php_mysql_conn *mysql;
    1734           3 :         zval *mysql_link = NULL;
    1735             :         MYSQL_RES *mysql_result;
    1736             : 
    1737           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1738           1 :                 return;
    1739             :         }
    1740             : 
    1741           2 :         if (!mysql_link) {
    1742           1 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1743           1 :                 CHECK_LINK(res);
    1744           1 :                 mysql = (php_mysql_conn*)res->ptr;
    1745             :         } else {
    1746           1 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1747             :         }
    1748             : 
    1749           2 :         PHPMY_UNBUFFERED_QUERY_CHECK();
    1750             : 
    1751           2 :         mysql_result = mysql_list_processes(mysql->conn);
    1752           2 :         if (mysql_result == NULL) {
    1753           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save MySQL query result");
    1754           0 :                 RETURN_FALSE;
    1755             :         }
    1756             : 
    1757           2 :         MySG(result_allocated)++;
    1758           2 :         ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
    1759             : }
    1760             : /* }}} */
    1761             : 
    1762             : /* {{{ proto string mysql_error([int link_identifier])
    1763             :    Returns the text of the error message from previous MySQL operation */
    1764          20 : PHP_FUNCTION(mysql_error)
    1765             : {
    1766             :         php_mysql_conn *mysql;
    1767          20 :         zval *mysql_link = NULL;
    1768             : 
    1769          20 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1770           2 :                 return;
    1771             :         }
    1772             : 
    1773          18 :         if (!mysql_link) {
    1774           8 :                 if (MySG(default_link) == NULL) {
    1775           3 :                         if (MySG(connect_error) != NULL){
    1776           4 :                                 RETURN_STRING(MySG(connect_error));
    1777             :                         } else {
    1778           1 :                                 RETURN_FALSE;
    1779             :                         }
    1780             :                 }
    1781           5 :                 mysql = (php_mysql_conn*)MySG(default_link)->ptr;
    1782             :         } else {
    1783          10 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1784             :         }
    1785             : 
    1786          28 :         RETURN_STRING((char *)mysql_error(mysql->conn));
    1787             : }
    1788             : /* }}} */
    1789             : 
    1790             : /* {{{ proto int mysql_errno([int link_identifier])
    1791             :    Returns the number of the error message from previous MySQL operation */
    1792             : #ifdef HAVE_MYSQL_ERRNO
    1793          22 : PHP_FUNCTION(mysql_errno)
    1794             : {
    1795             :         php_mysql_conn *mysql;
    1796          22 :         zval *mysql_link = NULL;
    1797             : 
    1798          22 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1799           2 :                 return;
    1800             :         }
    1801             : 
    1802          20 :         if (!mysql_link) {
    1803           8 :                 if (MySG(default_link) == NULL) {
    1804           3 :                         if (MySG(connect_error) != NULL){
    1805           2 :                                 RETURN_LONG(MySG(connect_errno));
    1806             :                         } else {
    1807           1 :                                 RETURN_FALSE;
    1808             :                         }
    1809             :                 }
    1810           5 :                 mysql = (php_mysql_conn*)MySG(default_link)->ptr;
    1811             :         } else {
    1812          12 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1813             :         }
    1814             : 
    1815          16 :         RETURN_LONG(mysql_errno(mysql->conn));
    1816             : }
    1817             : #endif
    1818             : /* }}} */
    1819             : 
    1820             : /* {{{ proto int mysql_affected_rows([int link_identifier])
    1821             :    Gets number of affected rows in previous MySQL operation */
    1822          17 : PHP_FUNCTION(mysql_affected_rows)
    1823             : {
    1824             :         php_mysql_conn *mysql;
    1825          17 :         zval *mysql_link = NULL;
    1826             : 
    1827          17 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1828           2 :                 return;
    1829             :         }
    1830             : 
    1831          15 :         if (!mysql_link) {
    1832           2 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1833           2 :                 CHECK_LINK(res);
    1834           2 :                 mysql = (php_mysql_conn*)res->ptr;
    1835             :         } else {
    1836          13 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1837             :         }
    1838             : 
    1839             :         /* conversion from int64 to long happing here */
    1840          14 :         RETURN_LONG((zend_long)mysql_affected_rows(mysql->conn));
    1841             : }
    1842             : /* }}} */
    1843             : 
    1844             : /* {{{ proto string mysql_escape_string(string to_be_escaped)
    1845             :    Escape string for mysql query */
    1846          10 : PHP_FUNCTION(mysql_escape_string)
    1847             : {
    1848             :         char *str;
    1849             :         size_t str_len;
    1850             :         zend_string *escaped_str;
    1851             : 
    1852          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
    1853           1 :                 return;
    1854             :         }
    1855             : 
    1856             :         /* assume worst case situation, which is 2x of the original string.
    1857             :          * we don't realloc() down to the real size since it'd most probably not
    1858             :          * be worth it
    1859             :          */
    1860          18 :         escaped_str = zend_string_alloc(str_len * 2, 0);
    1861           9 :         escaped_str->len = mysql_escape_string(escaped_str->val, str, str_len);
    1862             : 
    1863           9 :         php_error_docref("function.mysql-real-escape-string" TSRMLS_CC, E_DEPRECATED, "This function is deprecated; use mysql_real_escape_string() instead.");
    1864           9 :         RETURN_STR(escaped_str);
    1865             : }
    1866             : /* }}} */
    1867             : 
    1868             : /* {{{ proto string mysql_real_escape_string(string to_be_escaped [, int link_identifier])
    1869             :         Escape special characters in a string for use in a SQL statement, taking into account the current charset of the connection */
    1870          17 : PHP_FUNCTION(mysql_real_escape_string)
    1871             : {
    1872             :         char *str;
    1873             :         size_t str_len;
    1874             :         zend_string *new_str;
    1875             :         php_mysql_conn *mysql;
    1876          17 :         zval *mysql_link = NULL;
    1877             : 
    1878          17 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &str, &str_len, &mysql_link) == FAILURE) {
    1879           2 :                 return;
    1880             :         }
    1881             : 
    1882          15 :         if (!mysql_link) {
    1883           1 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1884           1 :                 CHECK_LINK(res);
    1885           1 :                 mysql = (php_mysql_conn*)res->ptr;
    1886             :         } else {
    1887          14 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1888             :         }
    1889             : 
    1890             :         /* assume worst case situation, which is 2x of the original string.
    1891             :          * we don't realloc() down to the real size since it'd most probably not
    1892             :          * be worth it
    1893             :          */
    1894          30 :         new_str = zend_string_alloc(str_len * 2, 0);
    1895          15 :         new_str->len = mysql_real_escape_string(mysql->conn, new_str->val, str, str_len);
    1896             : 
    1897          15 :         RETURN_NEW_STR(new_str);
    1898             : }
    1899             : /* }}} */
    1900             : 
    1901             : /* {{{ proto int mysql_insert_id([int link_identifier])
    1902             :    Gets the ID generated from the previous INSERT operation */
    1903           9 : PHP_FUNCTION(mysql_insert_id)
    1904             : {
    1905             :         php_mysql_conn *mysql;
    1906           9 :         zval *mysql_link = NULL;
    1907             : 
    1908           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1909           2 :                 return;
    1910             :         }
    1911             : 
    1912           7 :         if (!mysql_link) {
    1913           2 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1914           2 :                 CHECK_LINK(res);
    1915           2 :                 mysql = (php_mysql_conn*)res->ptr;
    1916             :         } else {
    1917           5 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    1918             :         }
    1919             : 
    1920             :         /* conversion from int64 to long happing here */
    1921           6 :         RETURN_LONG((zend_long)mysql_insert_id(mysql->conn));
    1922             : }
    1923             : /* }}} */
    1924             : 
    1925             : /* {{{ proto mixed mysql_result(resource result, int row [, mixed field])
    1926             :    Gets result data */
    1927          36 : PHP_FUNCTION(mysql_result)
    1928             : {
    1929          36 :         zval *result, *field=NULL;
    1930             :         zend_long row;
    1931             :         MYSQL_RES *mysql_result;
    1932             : #ifndef MYSQL_USE_MYSQLND
    1933             :         MYSQL_ROW sql_row;
    1934             :         mysql_row_length_type *sql_row_lengths;
    1935             : #endif
    1936          36 :         int field_offset=0;
    1937             : 
    1938             : /*
    1939             : johannes TODO:
    1940             : Do 2 zend_parse_parameters calls instead of type "z" and switch below
    1941             : Q: String or long first?
    1942             : */
    1943          36 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|z", &result, &row, &field) == FAILURE) {
    1944           7 :                 return;
    1945             :         }
    1946             : 
    1947          29 :         ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
    1948             : 
    1949          26 :         if (row < 0 || row >= (int)mysql_num_rows(mysql_result)) {
    1950           6 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %pd on MySQL result index %pd", row, Z_RES_P(result)->handle);
    1951           6 :                 RETURN_FALSE;
    1952             :         }
    1953          20 :         mysql_data_seek(mysql_result, row);
    1954             : 
    1955          20 :         if (field) {
    1956          32 :                 switch (Z_TYPE_P(field)) {
    1957             :                         case IS_STRING: {
    1958          13 :                                         int i = 0;
    1959             :                                         const MYSQL_FIELD *tmp_field;
    1960             :                                         char *table_name, *field_name, *tmp;
    1961             : 
    1962          13 :                                         if ((tmp = strchr(Z_STRVAL_P(field), '.'))) {
    1963           6 :                                                 table_name = estrndup(Z_STRVAL_P(field), tmp-Z_STRVAL_P(field));
    1964           6 :                                                 field_name = estrdup(tmp + 1);
    1965             :                                         } else {
    1966           7 :                                                 table_name = NULL;
    1967           7 :                                                 field_name = estrndup(Z_STRVAL_P(field),Z_STRLEN_P(field));
    1968             :                                         }
    1969          13 :                                         mysql_field_seek(mysql_result, 0);
    1970          67 :                                         while ((tmp_field = mysql_fetch_field(mysql_result))) {
    1971         113 :                                                 if ((!table_name ||
    1972          23 :                                                                         !strncasecmp(tmp_field->table, table_name, tmp_field->table_length)) &&
    1973          42 :                                                                 !strncasecmp(tmp_field->name, field_name, tmp_field->name_length)) {
    1974           7 :                                                         field_offset = i;
    1975           7 :                                                         break;
    1976             :                                                 }
    1977          41 :                                                 i++;
    1978             :                                         }
    1979          13 :                                         if (!tmp_field) { /* no match found */
    1980           6 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s%s%s not found in MySQL result index %pd",
    1981           6 :                                                                         (table_name?table_name:""), (table_name?".":""), field_name, Z_RES_P(result)->handle);
    1982           6 :                                                 efree(field_name);
    1983           6 :                                                 if (table_name) {
    1984           4 :                                                         efree(table_name);
    1985             :                                                 }
    1986           6 :                                                 RETURN_FALSE;
    1987             :                                         }
    1988           7 :                                         efree(field_name);
    1989           7 :                                         if (table_name) {
    1990           2 :                                                 efree(table_name);
    1991             :                                         }
    1992             :                                 }
    1993           7 :                                 break;
    1994             :                         default:
    1995           6 :                                 convert_to_long_ex(field);
    1996           3 :                                 field_offset = Z_LVAL_P(field);
    1997           3 :                                 if (field_offset < 0 || field_offset >= (int)mysql_num_fields(mysql_result)) {
    1998           2 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified");
    1999           2 :                                         RETURN_FALSE;
    2000             :                                 }
    2001             :                                 break;
    2002             :                 }
    2003             :         }
    2004             : 
    2005             : #ifndef MYSQL_USE_MYSQLND
    2006             :         if ((sql_row = mysql_fetch_row(mysql_result)) == NULL
    2007             :                 || (sql_row_lengths=mysql_fetch_lengths(mysql_result)) == NULL) { /* shouldn't happen? */
    2008             :                 RETURN_FALSE;
    2009             :         }
    2010             :         if (sql_row[field_offset]) {
    2011             : #if PHP_API_VERSION < 20100412
    2012             :                 if (PG(magic_quotes_runtime)) {
    2013             :                         RETVAL_STR(php_addslashes(sql_row[field_offset], sql_row_lengths[field_offset], 0 TSRMLS_CC));
    2014             :                 } else {
    2015             : #endif
    2016             :                         RETVAL_STRINGL(sql_row[field_offset], sql_row_lengths[field_offset]);
    2017             : #if PHP_API_VERSION < 20100412
    2018             :                 }
    2019             : #endif
    2020             :         } else {
    2021             :                 RETURN_NULL();
    2022             :         }
    2023             : #else
    2024          12 :         mysqlnd_result_fetch_field_data(mysql_result, field_offset, return_value);
    2025             : #endif
    2026             : }
    2027             : /* }}} */
    2028             : 
    2029             : /* {{{ proto int mysql_num_rows(resource result)
    2030             :    Gets number of rows in a result */
    2031          16 : PHP_FUNCTION(mysql_num_rows)
    2032             : {
    2033             :         zval *result;
    2034             :         MYSQL_RES *mysql_result;
    2035             : 
    2036          16 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
    2037           3 :                 return;
    2038             :         }
    2039             : 
    2040          13 :         ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
    2041             : 
    2042             :         /* conversion from int64 to long happing here */
    2043          12 :         RETURN_LONG((zend_long) mysql_num_rows(mysql_result));
    2044             : }
    2045             : /* }}} */
    2046             : 
    2047             : /* {{{ proto int mysql_num_fields(resource result)
    2048             :    Gets number of fields in a result */
    2049          16 : PHP_FUNCTION(mysql_num_fields)
    2050             : {
    2051             :         zval *result;
    2052             :         MYSQL_RES *mysql_result;
    2053             : 
    2054          16 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
    2055           2 :                 return;
    2056             :         }
    2057             : 
    2058          14 :         ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
    2059             : 
    2060          13 :         RETURN_LONG(mysql_num_fields(mysql_result));
    2061             : }
    2062             : /* }}} */
    2063             : 
    2064             : /* {{{ php_mysql_fetch_hash
    2065             :  */
    2066         194 : static void php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_type, int expected_args, int into_object)
    2067             : {
    2068             :         MYSQL_RES *mysql_result;
    2069         194 :         zval *res, *ctor_params = NULL;
    2070         194 :         zend_class_entry *ce = NULL;
    2071             : #ifndef MYSQL_USE_MYSQLND
    2072             :         int i;
    2073             :         MYSQL_FIELD *mysql_field;
    2074             :         MYSQL_ROW mysql_row;
    2075             :         mysql_row_length_type *mysql_row_lengths;
    2076             : #endif
    2077             : 
    2078             : #ifdef ZEND_ENGINE_2
    2079         194 :         if (into_object) {
    2080          22 :                 zend_string *class_name = NULL;
    2081             : 
    2082          22 :                 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|Sz", &res, &class_name, &ctor_params) == FAILURE) {
    2083           1 :                         return;
    2084             :                 }
    2085             : 
    2086          21 :                 if (ZEND_NUM_ARGS() < 2) {
    2087           8 :                         ce = zend_standard_class_def;
    2088             :                 } else {
    2089          13 :                         ce = zend_fetch_class(class_name, ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
    2090             :                 }
    2091          20 :                 if (!ce) {
    2092           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not find class '%s'", class_name->val);
    2093           0 :                         return;
    2094             :                 }
    2095          20 :                 result_type = MYSQL_ASSOC;
    2096             :         } else
    2097             : #endif
    2098             :         {
    2099         172 :                 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &res, &result_type) == FAILURE) {
    2100           6 :                         return;
    2101             :                 }
    2102         166 :                 if (!result_type) {
    2103             :                         /* result_type might have been set outside, so only overwrite when not set */
    2104           2 :                         result_type = MYSQL_BOTH;
    2105             :                 }
    2106             :         }
    2107             : 
    2108         186 :         if (result_type & ~MYSQL_BOTH) {
    2109           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The result type should be either MYSQL_NUM, MYSQL_ASSOC or MYSQL_BOTH");
    2110           2 :                 result_type = MYSQL_BOTH;
    2111             :         }
    2112             : 
    2113         186 :         ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, res, -1, "MySQL result", le_result);
    2114             : 
    2115             : #ifndef MYSQL_USE_MYSQLND
    2116             :         if ((mysql_row = mysql_fetch_row(mysql_result)) == NULL  ||
    2117             :                 (mysql_row_lengths = mysql_fetch_lengths(mysql_result)) == NULL) {
    2118             :                 RETURN_FALSE;
    2119             :         }
    2120             : 
    2121             :         array_init(return_value);
    2122             : 
    2123             :         mysql_field_seek(mysql_result, 0);
    2124             :         for (mysql_field = mysql_fetch_field(mysql_result), i = 0;
    2125             :                  mysql_field;
    2126             :                  mysql_field = mysql_fetch_field(mysql_result), i++)
    2127             :         {
    2128             :                 if (mysql_row[i]) {
    2129             :                         zval data;
    2130             : 
    2131             : #if PHP_API_VERSION < 20100412
    2132             :                         if (PG(magic_quotes_runtime)) {
    2133             :                                 ZVAL_STR(&data, php_addslashes(mysql_row[i], mysql_row_lengths[i], 0 TSRMLS_CC));
    2134             :                         } else {
    2135             : #endif
    2136             :                                 ZVAL_STRINGL(&data, mysql_row[i], mysql_row_lengths[i]);
    2137             : #if PHP_API_VERSION < 20100412
    2138             :                         }
    2139             : #endif
    2140             : 
    2141             :                         if (result_type & MYSQL_NUM) {
    2142             :                                 add_index_zval(return_value, i, &data);
    2143             :                         }
    2144             :                         if (result_type & MYSQL_ASSOC) {
    2145             :                                 if (result_type & MYSQL_NUM) {
    2146             :                                         Z_ADDREF_P(&data);
    2147             :                                 }
    2148             :                                 add_assoc_zval(return_value, mysql_field->name, &data);
    2149             :                         }
    2150             :                 } else {
    2151             :                         /* NULL value. */
    2152             :                         if (result_type & MYSQL_NUM) {
    2153             :                                 add_index_null(return_value, i);
    2154             :                         }
    2155             : 
    2156             :                         if (result_type & MYSQL_ASSOC) {
    2157             :                                 add_assoc_null(return_value, mysql_field->name);
    2158             :                         }
    2159             :                 }
    2160             :         }
    2161             : #else
    2162         181 :         mysqlnd_fetch_into(mysql_result, ((result_type & MYSQL_NUM)? MYSQLND_FETCH_NUM:0) | ((result_type & MYSQL_ASSOC)? MYSQLND_FETCH_ASSOC:0), return_value, MYSQLND_MYSQL);
    2163             : #endif
    2164             : 
    2165             : #ifdef ZEND_ENGINE_2
    2166             :         /* mysqlnd might return FALSE if no more rows */
    2167         199 :         if (into_object && Z_TYPE_P(return_value) != IS_FALSE) {
    2168             :                 zval dataset;
    2169             :                 zend_fcall_info fci;
    2170             :                 zend_fcall_info_cache fcc;
    2171             :                 zval retval;
    2172             : 
    2173          11 :                 ZVAL_COPY_VALUE(&dataset, return_value);
    2174          11 :                 object_and_properties_init(return_value, ce, NULL);
    2175          18 :                 if (!ce->default_properties_count && !ce->__set) {
    2176           7 :                         ALLOC_HASHTABLE(Z_OBJ_P(return_value)->properties);
    2177           7 :                         *Z_OBJ_P(return_value)->properties = *Z_ARRVAL(dataset);
    2178           7 :                         efree(Z_ARR(dataset));
    2179             :                 } else {
    2180           4 :                         zend_merge_properties(return_value, Z_ARRVAL(dataset) TSRMLS_CC);
    2181             :                         zval_dtor(&dataset);
    2182             :                 }
    2183             : 
    2184          11 :                 if (ce->constructor) {
    2185           6 :                         fci.size = sizeof(fci);
    2186           6 :                         fci.function_table = &ce->function_table;
    2187           6 :                         ZVAL_UNDEF(&fci.function_name);
    2188           6 :                         fci.symbol_table = NULL;
    2189           6 :                         fci.object = Z_OBJ_P(return_value);
    2190           6 :                         fci.retval = &retval;
    2191           6 :                         fci.params = NULL;
    2192           6 :                         fci.param_count = 0;
    2193           6 :                         fci.no_separation = 1;
    2194             : 
    2195           9 :                         if (ctor_params && Z_TYPE_P(ctor_params) != IS_NULL) {
    2196           2 :                                 if (zend_fcall_info_args(&fci, ctor_params TSRMLS_CC) == FAILURE) {
    2197             :                                         /* Two problems why we throw exceptions here: PHP is typeless
    2198             :                                          * and hence passing one argument that's not an array could be
    2199             :                                          * by mistake and the other way round is possible, too. The
    2200             :                                          * single value is an array. Also we'd have to make that one
    2201             :                                          * argument passed by reference.
    2202             :                                          */
    2203           0 :                                         zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Parameter ctor_params must be an array", 0 TSRMLS_CC);
    2204           0 :                                         return;
    2205             :                                 }
    2206             :                         }
    2207             : 
    2208           6 :                         fcc.initialized = 1;
    2209           6 :                         fcc.function_handler = ce->constructor;
    2210           6 :                         fcc.calling_scope = EG(scope);
    2211           6 :                         fcc.called_scope = Z_OBJCE_P(return_value);
    2212           6 :                         fcc.object = Z_OBJ_P(return_value);
    2213             : 
    2214           6 :                         if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
    2215           0 :                                 zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Could not execute %s::%s()", ce->name->val, ce->constructor->common.function_name->val);
    2216             :                         } else {
    2217           6 :                                 if (!Z_ISUNDEF(retval)) {
    2218           6 :                                         zval_ptr_dtor(&retval);
    2219             :                                 }
    2220             :                         }
    2221           6 :                         if (fci.params) {
    2222           2 :                                 efree(fci.params);
    2223             :                         }
    2224           5 :                 } else if (ctor_params) {
    2225           0 :                         zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Class %s does not have a constructor hence you cannot use ctor_params", ce->name->val);
    2226             :                 }
    2227             :         }
    2228             : #endif
    2229             : 
    2230             : }
    2231             : /* }}} */
    2232             : 
    2233             : /* {{{ proto array mysql_fetch_row(resource result)
    2234             :    Gets a result row as an enumerated array */
    2235           5 : PHP_FUNCTION(mysql_fetch_row)
    2236             : {
    2237           5 :         php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_NUM, 1, 0);
    2238           5 : }
    2239             : /* }}} */
    2240             : 
    2241             : /* {{{ proto object mysql_fetch_object(resource result [, string class_name [, NULL|array ctor_params]])
    2242             :    Fetch a result row as an object */
    2243          22 : PHP_FUNCTION(mysql_fetch_object)
    2244             : {
    2245          22 :         php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_ASSOC, 2, 1);
    2246             : 
    2247          21 :         if (Z_TYPE_P(return_value) == IS_ARRAY) {
    2248           0 :                 object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));
    2249             :         }
    2250          21 : }
    2251             : /* }}} */
    2252             : 
    2253             : /* {{{ proto array mysql_fetch_array(resource result [, int result_type])
    2254             :    Fetch a result row as an array (associative, numeric or both) */
    2255         103 : PHP_FUNCTION(mysql_fetch_array)
    2256             : {
    2257         103 :         php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 2, 0);
    2258         103 : }
    2259             : /* }}} */
    2260             : 
    2261             : /* {{{ proto array mysql_fetch_assoc(resource result)
    2262             :    Fetch a result row as an associative array */
    2263          64 : PHP_FUNCTION(mysql_fetch_assoc)
    2264             : {
    2265          64 :         php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_ASSOC, 1, 0);
    2266          64 : }
    2267             : /* }}} */
    2268             : 
    2269             : /* {{{ proto bool mysql_data_seek(resource result, int row_number)
    2270             :    Move internal result pointer */
    2271           9 : PHP_FUNCTION(mysql_data_seek)
    2272             : {
    2273             :         zval *result;
    2274             :         zend_long offset;
    2275             :         MYSQL_RES *mysql_result;
    2276             : 
    2277           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &offset)) {
    2278           3 :                 return;
    2279             :         }
    2280             : 
    2281           6 :         ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
    2282             : 
    2283           5 :         if (offset < 0 || offset >= (int)mysql_num_rows(mysql_result)) {
    2284           3 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset %pd is invalid for MySQL result index %pd (or the query data is unbuffered)", offset, Z_RES_P(result)->handle);
    2285           3 :                 RETURN_FALSE;
    2286             :         }
    2287           2 :         mysql_data_seek(mysql_result, offset);
    2288           2 :         RETURN_TRUE;
    2289             : }
    2290             : /* }}} */
    2291             : 
    2292             : /* {{{ proto array mysql_fetch_lengths(resource result)
    2293             :    Gets max data size of each column in a result */
    2294           5 : PHP_FUNCTION(mysql_fetch_lengths)
    2295             : {
    2296             :         zval *result;
    2297             :         MYSQL_RES *mysql_result;
    2298             :         mysql_row_length_type *lengths;
    2299             :         int num_fields;
    2300             :         int i;
    2301             : 
    2302           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
    2303           2 :                 return;
    2304             :         }
    2305             : 
    2306           3 :         ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
    2307             : 
    2308           2 :         if ((lengths=mysql_fetch_lengths(mysql_result))==NULL) {
    2309           1 :                 RETURN_FALSE;
    2310             :         }
    2311           1 :         array_init(return_value);
    2312           1 :         num_fields = mysql_num_fields(mysql_result);
    2313             : 
    2314           3 :         for (i=0; i<num_fields; i++) {
    2315           2 :                 add_index_long(return_value, i, lengths[i]);
    2316             :         }
    2317             : }
    2318             : /* }}} */
    2319             : 
    2320             : /* {{{ php_mysql_get_field_name
    2321             :  */
    2322          42 : static char *php_mysql_get_field_name(int field_type)
    2323             : {
    2324          42 :         switch(field_type) {
    2325             :                 case FIELD_TYPE_STRING:
    2326             :                 case FIELD_TYPE_VAR_STRING:
    2327           9 :                         return "string";
    2328             :                         break;
    2329             : #if MYSQL_VERSION_ID > 50002 || defined(MYSQL_USE_MYSQLND)
    2330             :                 case MYSQL_TYPE_BIT:
    2331             : #endif
    2332             : #ifdef MYSQL_HAS_TINY
    2333             :                 case FIELD_TYPE_TINY:
    2334             : #endif
    2335             :                 case FIELD_TYPE_SHORT:
    2336             :                 case FIELD_TYPE_LONG:
    2337             :                 case FIELD_TYPE_LONGLONG:
    2338             :                 case FIELD_TYPE_INT24:
    2339          17 :                         return "int";
    2340             :                         break;
    2341             :                 case FIELD_TYPE_FLOAT:
    2342             :                 case FIELD_TYPE_DOUBLE:
    2343             :                 case FIELD_TYPE_DECIMAL:
    2344             : #ifdef FIELD_TYPE_NEWDECIMAL
    2345             :                 case FIELD_TYPE_NEWDECIMAL:
    2346             : #endif
    2347           3 :                         return "real";
    2348             :                         break;
    2349             :                 case FIELD_TYPE_TIMESTAMP:
    2350           1 :                         return "timestamp";
    2351             :                         break;
    2352             : #ifdef MYSQL_HAS_YEAR
    2353             :                 case FIELD_TYPE_YEAR:
    2354           1 :                         return "year";
    2355             :                         break;
    2356             : #endif
    2357             :                 case FIELD_TYPE_DATE:
    2358             : #ifdef FIELD_TYPE_NEWDATE
    2359             :                 case FIELD_TYPE_NEWDATE:
    2360             : #endif
    2361           1 :                         return "date";
    2362             :                         break;
    2363             :                 case FIELD_TYPE_TIME:
    2364           1 :                         return "time";
    2365             :                         break;
    2366             :                 case FIELD_TYPE_SET:
    2367           0 :                         return "set";
    2368             :                         break;
    2369             :                 case FIELD_TYPE_ENUM:
    2370           0 :                         return "enum";
    2371             :                         break;
    2372             : #ifdef FIELD_TYPE_GEOMETRY
    2373             :                 case FIELD_TYPE_GEOMETRY:
    2374           0 :                         return "geometry";
    2375             :                         break;
    2376             : #endif
    2377             :                 case FIELD_TYPE_DATETIME:
    2378           1 :                         return "datetime";
    2379             :                         break;
    2380             :                 case FIELD_TYPE_TINY_BLOB:
    2381             :                 case FIELD_TYPE_MEDIUM_BLOB:
    2382             :                 case FIELD_TYPE_LONG_BLOB:
    2383             :                 case FIELD_TYPE_BLOB:
    2384           8 :                         return "blob";
    2385             :                         break;
    2386             :                 case FIELD_TYPE_NULL:
    2387           0 :                         return "null";
    2388             :                         break;
    2389             :                 default:
    2390           0 :                         return "unknown";
    2391             :                         break;
    2392             :         }
    2393             : }
    2394             : /* }}} */
    2395             : 
    2396             : /* {{{ proto object mysql_fetch_field(resource result [, int field_offset])
    2397             :    Gets column information from a result and return as an object */
    2398          46 : PHP_FUNCTION(mysql_fetch_field)
    2399             : {
    2400             :         zval *result;
    2401          46 :         zend_long field=0;
    2402             :         MYSQL_RES *mysql_result;
    2403             :         const MYSQL_FIELD *mysql_field;
    2404             : 
    2405          46 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &result, &field) == FAILURE) {
    2406           2 :                 return;
    2407             :         }
    2408             : 
    2409          44 :         ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
    2410             : 
    2411          43 :         if (ZEND_NUM_ARGS() > 1) {
    2412          33 :                 if (field<0 || field >= (int)mysql_num_fields(mysql_result)) {
    2413           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad field offset");
    2414           1 :                         RETURN_FALSE;
    2415             :                 }
    2416          32 :                 mysql_field_seek(mysql_result, field);
    2417             :         }
    2418          42 :         if ((mysql_field = mysql_fetch_field(mysql_result)) == NULL) {
    2419           3 :                 RETURN_FALSE;
    2420             :         }
    2421          39 :         object_init(return_value);
    2422             : 
    2423             : #if MYSQL_USE_MYSQLND
    2424          78 :         add_property_str(return_value, "name", zend_string_copy(mysql_field->sname));
    2425             : #else
    2426             :         add_property_stringl(return_value, "name", (mysql_field->name?mysql_field->name:""), mysql_field->name_length);
    2427             : #endif
    2428          39 :         add_property_stringl(return_value, "table", (mysql_field->table?mysql_field->table:""), mysql_field->table_length);
    2429          39 :         add_property_stringl(return_value, "def", (mysql_field->def?mysql_field->def:""), mysql_field->def_length);
    2430          39 :         add_property_long(return_value, "max_length", mysql_field->max_length);
    2431          39 :         add_property_long(return_value, "not_null", IS_NOT_NULL(mysql_field->flags)?1:0);
    2432          39 :         add_property_long(return_value, "primary_key", IS_PRI_KEY(mysql_field->flags)?1:0);
    2433          39 :         add_property_long(return_value, "multiple_key", (mysql_field->flags&MULTIPLE_KEY_FLAG?1:0));
    2434          39 :         add_property_long(return_value, "unique_key", (mysql_field->flags&UNIQUE_KEY_FLAG?1:0));
    2435          39 :         add_property_long(return_value, "numeric", IS_NUM(mysql_field->type)?1:0);
    2436          39 :         add_property_long(return_value, "blob", IS_BLOB(mysql_field->flags)?1:0);
    2437          39 :         add_property_string(return_value, "type", php_mysql_get_field_name(mysql_field->type));
    2438          39 :         add_property_long(return_value, "unsigned", (mysql_field->flags&UNSIGNED_FLAG?1:0));
    2439          39 :         add_property_long(return_value, "zerofill", (mysql_field->flags&ZEROFILL_FLAG?1:0));
    2440             : }
    2441             : /* }}} */
    2442             : 
    2443             : /* {{{ proto bool mysql_field_seek(resource result, int field_offset)
    2444             :    Sets result pointer to a specific field offset */
    2445           8 : PHP_FUNCTION(mysql_field_seek)
    2446             : {
    2447             :         zval *result;
    2448             :         zend_long offset;
    2449             :         MYSQL_RES *mysql_result;
    2450             : 
    2451           8 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &offset) == FAILURE) {
    2452           2 :                 return;
    2453             :         }
    2454           6 :         ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
    2455             : 
    2456           5 :         if (offset < 0 || offset >= (int)mysql_num_fields(mysql_result)) {
    2457           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field %pd is invalid for MySQL result index %pd", offset, Z_RES_P(result)->handle);
    2458           2 :                 RETURN_FALSE;
    2459             :         }
    2460           3 :         mysql_field_seek(mysql_result, offset);
    2461           3 :         RETURN_TRUE;
    2462             : }
    2463             : /* }}} */
    2464             : 
    2465             : #define PHP_MYSQL_FIELD_NAME 1
    2466             : #define PHP_MYSQL_FIELD_TABLE 2
    2467             : #define PHP_MYSQL_FIELD_LEN 3
    2468             : #define PHP_MYSQL_FIELD_TYPE 4
    2469             : #define PHP_MYSQL_FIELD_FLAGS 5
    2470             : 
    2471             : /* {{{ php_mysql_field_info
    2472             :  */
    2473          50 : static void php_mysql_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
    2474             : {
    2475             :         zval *result;
    2476             :         zend_long field;
    2477             :         MYSQL_RES *mysql_result;
    2478          50 :         const MYSQL_FIELD *mysql_field = {0};
    2479             :         char buf[512];
    2480             :         int  len;
    2481             : 
    2482          50 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &field) == FAILURE) {
    2483          15 :                 return;
    2484             :         }
    2485             : 
    2486          35 :         ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
    2487             : 
    2488          30 :         if (field < 0 || field >= (int)mysql_num_fields(mysql_result)) {
    2489          10 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field %pd is invalid for MySQL result index %pd", field, Z_RES_P(result)->handle);
    2490          10 :                 RETURN_FALSE;
    2491             :         }
    2492          20 :         mysql_field_seek(mysql_result, field);
    2493          20 :         if ((mysql_field=mysql_fetch_field(mysql_result))==NULL) {
    2494           0 :                 RETURN_FALSE;
    2495             :         }
    2496             : 
    2497          20 :         switch (entry_type) {
    2498             :                 case PHP_MYSQL_FIELD_NAME:
    2499             : #ifdef MYSQL_USE_MYSQLND
    2500           6 :                         RETVAL_STR(zend_string_copy(mysql_field->sname));
    2501             : #else
    2502             :                         RETVAL_STRING(mysql_field->name);
    2503             : #endif
    2504           3 :                         break;
    2505             :                 case PHP_MYSQL_FIELD_TABLE:
    2506           2 :                         RETVAL_STRING(mysql_field->table);
    2507           1 :                         break;
    2508             :                 case PHP_MYSQL_FIELD_LEN:
    2509           3 :                         RETVAL_LONG(mysql_field->length);
    2510           3 :                         break;
    2511             :                 case PHP_MYSQL_FIELD_TYPE:
    2512           6 :                         RETVAL_STRING(php_mysql_get_field_name(mysql_field->type));
    2513           3 :                         break;
    2514             :                 case PHP_MYSQL_FIELD_FLAGS:
    2515          10 :                         memcpy(buf, "", sizeof(""));
    2516             : #ifdef IS_NOT_NULL
    2517          10 :                         if (IS_NOT_NULL(mysql_field->flags)) {
    2518           3 :                                 strcat(buf, "not_null ");
    2519             :                         }
    2520             : #endif
    2521             : #ifdef IS_PRI_KEY
    2522          10 :                         if (IS_PRI_KEY(mysql_field->flags)) {
    2523           2 :                                 strcat(buf, "primary_key ");
    2524             :                         }
    2525             : #endif
    2526             : #ifdef UNIQUE_KEY_FLAG
    2527          10 :                         if (mysql_field->flags&UNIQUE_KEY_FLAG) {
    2528           1 :                                 strcat(buf, "unique_key ");
    2529             :                         }
    2530             : #endif
    2531             : #ifdef MULTIPLE_KEY_FLAG
    2532          10 :                         if (mysql_field->flags&MULTIPLE_KEY_FLAG) {
    2533           1 :                                 strcat(buf, "multiple_key ");
    2534             :                         }
    2535             : #endif
    2536             : #ifdef IS_BLOB
    2537          10 :                         if (IS_BLOB(mysql_field->flags)) {
    2538           1 :                                 strcat(buf, "blob ");
    2539             :                         }
    2540             : #endif
    2541             : #ifdef UNSIGNED_FLAG
    2542          10 :                         if (mysql_field->flags&UNSIGNED_FLAG) {
    2543           2 :                                 strcat(buf, "unsigned ");
    2544             :                         }
    2545             : #endif
    2546             : #ifdef ZEROFILL_FLAG
    2547          10 :                         if (mysql_field->flags&ZEROFILL_FLAG) {
    2548           1 :                                 strcat(buf, "zerofill ");
    2549             :                         }
    2550             : #endif
    2551             : #ifdef BINARY_FLAG
    2552          10 :                         if (mysql_field->flags&BINARY_FLAG) {
    2553           2 :                                 strcat(buf, "binary ");
    2554             :                         }
    2555             : #endif
    2556             : #ifdef ENUM_FLAG
    2557          10 :                         if (mysql_field->flags&ENUM_FLAG) {
    2558           1 :                                 strcat(buf, "enum ");
    2559             :                         }
    2560             : #endif
    2561             : #ifdef SET_FLAG
    2562          10 :                         if (mysql_field->flags&SET_FLAG) {
    2563           1 :                                 strcat(buf, "set ");
    2564             :                         }
    2565             : #endif
    2566             : #ifdef AUTO_INCREMENT_FLAG
    2567          10 :                         if (mysql_field->flags&AUTO_INCREMENT_FLAG) {
    2568           0 :                                 strcat(buf, "auto_increment ");
    2569             :                         }
    2570             : #endif
    2571             : #ifdef TIMESTAMP_FLAG
    2572          10 :                         if (mysql_field->flags&TIMESTAMP_FLAG) {
    2573           1 :                                 strcat(buf, "timestamp ");
    2574             :                         }
    2575             : #endif
    2576          10 :                         len = strlen(buf);
    2577             :                         /* remove trailing space, if present */
    2578          10 :                         if (len && buf[len-1] == ' ') {
    2579           9 :                                 buf[len-1] = 0;
    2580           9 :                                 len--;
    2581             :                         }
    2582             : 
    2583          20 :                         RETVAL_STRINGL(buf, len);
    2584          10 :                         break;
    2585             : 
    2586             :                 default:
    2587           0 :                         RETURN_FALSE;
    2588             :         }
    2589             : }
    2590             : /* }}} */
    2591             : 
    2592             : /* {{{ proto string mysql_field_name(resource result, int field_index)
    2593             :    Gets the name of the specified field in a result */
    2594           9 : PHP_FUNCTION(mysql_field_name)
    2595             : {
    2596           9 :         php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_NAME);
    2597           9 : }
    2598             : /* }}} */
    2599             : 
    2600             : /* {{{ proto string mysql_field_table(resource result, int field_offset)
    2601             :    Gets name of the table the specified field is in */
    2602           7 : PHP_FUNCTION(mysql_field_table)
    2603             : {
    2604           7 :         php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_TABLE);
    2605           7 : }
    2606             : /* }}} */
    2607             : 
    2608             : /* {{{ proto int mysql_field_len(resource result, int field_offset)
    2609             :    Returns the length of the specified field */
    2610           9 : PHP_FUNCTION(mysql_field_len)
    2611             : {
    2612           9 :         php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_LEN);
    2613           9 : }
    2614             : /* }}} */
    2615             : 
    2616             : /* {{{ proto string mysql_field_type(resource result, int field_offset)
    2617             :    Gets the type of the specified field in a result */
    2618           9 : PHP_FUNCTION(mysql_field_type)
    2619             : {
    2620           9 :         php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_TYPE);
    2621           9 : }
    2622             : /* }}} */
    2623             : 
    2624             : /* {{{ proto string mysql_field_flags(resource result, int field_offset)
    2625             :    Gets the flags associated with the specified field in a result */
    2626          16 : PHP_FUNCTION(mysql_field_flags)
    2627             : {
    2628          16 :         php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_FLAGS);
    2629          16 : }
    2630             : /* }}} */
    2631             : 
    2632             : /* {{{ proto bool mysql_free_result(resource result)
    2633             :    Free result memory */
    2634         119 : PHP_FUNCTION(mysql_free_result)
    2635             : {
    2636             :         zval *result;
    2637             :         MYSQL_RES *mysql_result;
    2638             : 
    2639         119 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
    2640           3 :                 return;
    2641             :         }
    2642             : 
    2643         116 :         ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
    2644             : 
    2645         114 :         zend_list_close(Z_RES_P(result));
    2646         114 :         RETURN_TRUE;
    2647             : }
    2648             : /* }}} */
    2649             : 
    2650             : /* {{{ proto bool mysql_ping([int link_identifier])
    2651             :    Ping a server connection. If no connection then reconnect. */
    2652           6 : PHP_FUNCTION(mysql_ping)
    2653             : {
    2654             :         php_mysql_conn *mysql;
    2655           6 :         zval *mysql_link = NULL;
    2656             : 
    2657           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link)==FAILURE) {
    2658           1 :                 return;
    2659             :         }
    2660             : 
    2661           5 :         if (!mysql_link) {
    2662           1 :                 zend_resource *res = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    2663           1 :                 CHECK_LINK(res);
    2664           1 :                 mysql = (php_mysql_conn*)res->ptr;
    2665             :         } else {
    2666           4 :                 ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, -1, "MySQL-Link", le_link, le_plink);
    2667             :         }
    2668             : 
    2669           4 :         PHPMY_UNBUFFERED_QUERY_CHECK();
    2670             : 
    2671           4 :         RETURN_BOOL(!mysql_ping(mysql->conn));
    2672             : }
    2673             : /* }}} */
    2674             : 
    2675             : #endif
    2676             : 
    2677             : /*
    2678             :  * Local variables:
    2679             :  * tab-width: 4
    2680             :  * c-basic-offset: 4
    2681             :  * End:
    2682             :  * vim600: sw=4 ts=4 fdm=marker
    2683             :  * vim<600: sw=4 ts=4
    2684             :  */

Generated by: LCOV version 1.10

Generated at Tue, 14 Oct 2014 07:25:43 +0000 (7 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.