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

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

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

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:15 +0000 (8 days ago)

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