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: 804 861 93.4 %
Date: 2014-08-04 Functions: 63 64 98.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:06 +0000 (23 days ago)

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