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

Generated by: LCOV version 1.10

Generated at Sun, 18 Jan 2015 09:00:42 +0000 (7 days ago)

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