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

Generated by: LCOV version 1.10

Generated at Tue, 14 Apr 2015 11:48:45 +0000 (13 days ago)

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