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: 809 859 94.2 %
Date: 2014-07-23 Functions: 63 63 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 23 Jul 2014 19:58:33 +0000 (4 hours ago)

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