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

LTP GCOV extension - code coverage report
Current view: directory - mysql - php_mysql.c
Test: PHP Code Coverage
Date: 2008-09-05 Instrumented lines: 945
Code covered: 26.6 % Executed lines: 251
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2008 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: php_mysql.c,v 1.213.2.6.2.25 2008/04/16 08:54:44 tony2001 Exp $ */
      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                 : 
      37                 : #ifdef ZEND_ENGINE_2
      38                 : # include "zend_exceptions.h"
      39                 : #else
      40                 :   /* PHP 4 compat */
      41                 : # define OnUpdateLong   OnUpdateInt
      42                 : # define E_STRICT               E_NOTICE
      43                 : #endif
      44                 : 
      45                 : #if HAVE_MYSQL
      46                 : 
      47                 : #ifdef PHP_WIN32
      48                 : # include <winsock2.h>
      49                 : # define signal(a, b) NULL
      50                 : #elif defined(NETWARE)
      51                 : # include <sys/socket.h>
      52                 : # define signal(a, b) NULL
      53                 : #else
      54                 : # if HAVE_SIGNAL_H
      55                 : #  include <signal.h>
      56                 : # endif
      57                 : # if HAVE_SYS_TYPES_H
      58                 : #  include <sys/types.h>
      59                 : # endif
      60                 : # include <netdb.h>
      61                 : # include <netinet/in.h>
      62                 : # if HAVE_ARPA_INET_H
      63                 : #  include <arpa/inet.h>
      64                 : # endif
      65                 : #endif
      66                 : 
      67                 : #include <mysql.h>
      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
      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
      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                 : #if (MYSQL_VERSION_ID >= 40113 && MYSQL_VERSION_ID < 50000) || MYSQL_VERSION_ID >= 50007
     105                 : #define MYSQL_HAS_SET_CHARSET
     106                 : #endif
     107                 : 
     108                 : #define MYSQL_ASSOC             1<<0
     109                 : #define MYSQL_NUM               1<<1
     110                 : #define MYSQL_BOTH              (MYSQL_ASSOC|MYSQL_NUM)
     111                 : 
     112                 : #define MYSQL_USE_RESULT        0
     113                 : #define MYSQL_STORE_RESULT      1
     114                 : 
     115                 : #if MYSQL_VERSION_ID < 32224
     116                 : #define PHP_MYSQL_VALID_RESULT(mysql)           \
     117                 :         (mysql_num_fields(mysql)>0)
     118                 : #else
     119                 : #define PHP_MYSQL_VALID_RESULT(mysql)           \
     120                 :         (mysql_field_count(mysql)>0)
     121                 : #endif
     122                 : 
     123                 : ZEND_DECLARE_MODULE_GLOBALS(mysql)
     124                 : static PHP_GINIT_FUNCTION(mysql);
     125                 : 
     126                 : typedef struct _php_mysql_conn {
     127                 :         MYSQL conn;
     128                 :         int active_result_id;
     129                 : } php_mysql_conn;
     130                 : 
     131                 : /* {{{ mysql_functions[]
     132                 :  */
     133                 : zend_function_entry mysql_functions[] = {
     134                 :         PHP_FE(mysql_connect,                                                           NULL)
     135                 :         PHP_FE(mysql_pconnect,                                                          NULL)
     136                 :         PHP_FE(mysql_close,                                                                     NULL)
     137                 :         PHP_FE(mysql_select_db,                                                         NULL)
     138                 : #ifndef NETWARE         /* The below two functions not supported on NetWare */
     139                 : #if MYSQL_VERSION_ID < 40000
     140                 :         PHP_DEP_FE(mysql_create_db,                                                     NULL)
     141                 :         PHP_DEP_FE(mysql_drop_db,                                                       NULL)
     142                 : #endif
     143                 : #endif  /* NETWARE */
     144                 :         PHP_FE(mysql_query,                                                                     NULL)
     145                 :         PHP_FE(mysql_unbuffered_query,                                          NULL)
     146                 :         PHP_FE(mysql_db_query,                                                          NULL)
     147                 :         PHP_FE(mysql_list_dbs,                                                          NULL)
     148                 :         PHP_DEP_FE(mysql_list_tables,                                           NULL)
     149                 :         PHP_FE(mysql_list_fields,                                                       NULL)
     150                 :         PHP_FE(mysql_list_processes,                                            NULL)
     151                 :         PHP_FE(mysql_error,                                                                     NULL)
     152                 : #ifdef HAVE_MYSQL_ERRNO
     153                 :         PHP_FE(mysql_errno,                                                                     NULL)
     154                 : #endif
     155                 :         PHP_FE(mysql_affected_rows,                                                     NULL)
     156                 :         PHP_FE(mysql_insert_id,                                                         NULL)
     157                 :         PHP_FE(mysql_result,                                                            NULL)
     158                 :         PHP_FE(mysql_num_rows,                                                          NULL)
     159                 :         PHP_FE(mysql_num_fields,                                                        NULL)
     160                 :         PHP_FE(mysql_fetch_row,                                                         NULL)
     161                 :         PHP_FE(mysql_fetch_array,                                                       NULL)
     162                 :         PHP_FE(mysql_fetch_assoc,                                                       NULL)
     163                 :         PHP_FE(mysql_fetch_object,                                                      NULL)
     164                 :         PHP_FE(mysql_data_seek,                                                         NULL)
     165                 :         PHP_FE(mysql_fetch_lengths,                                                     NULL)
     166                 :         PHP_FE(mysql_fetch_field,                                                       NULL)
     167                 :         PHP_FE(mysql_field_seek,                                                        NULL)
     168                 :         PHP_FE(mysql_free_result,                                                       NULL)
     169                 :         PHP_FE(mysql_field_name,                                                        NULL)
     170                 :         PHP_FE(mysql_field_table,                                                       NULL)
     171                 :         PHP_FE(mysql_field_len,                                                         NULL)
     172                 :         PHP_FE(mysql_field_type,                                                        NULL)
     173                 :         PHP_FE(mysql_field_flags,                                                       NULL)
     174                 :         PHP_FE(mysql_escape_string,                                                     NULL)
     175                 :         PHP_FE(mysql_real_escape_string,                                        NULL)
     176                 :         PHP_FE(mysql_stat,                                                                      NULL)
     177                 :         PHP_FE(mysql_thread_id,                                                         NULL)
     178                 :         PHP_FE(mysql_client_encoding,                                   NULL)
     179                 :         PHP_FE(mysql_ping,                                                                      NULL)
     180                 : #ifdef HAVE_GETINFO_FUNCS
     181                 :         PHP_FE(mysql_get_client_info,                                           NULL)
     182                 :         PHP_FE(mysql_get_host_info,                                                     NULL)
     183                 :         PHP_FE(mysql_get_proto_info,                                            NULL)
     184                 :         PHP_FE(mysql_get_server_info,                                           NULL)
     185                 : #endif
     186                 : 
     187                 :         PHP_FE(mysql_info,                                                      NULL)
     188                 : #ifdef MYSQL_HAS_SET_CHARSET
     189                 :         PHP_FE(mysql_set_charset,                                                       NULL)
     190                 : #endif
     191                 :         /* for downwards compatability */
     192                 :         PHP_FALIAS(mysql,                               mysql_db_query,         NULL)
     193                 :         PHP_FALIAS(mysql_fieldname,             mysql_field_name,       NULL)
     194                 :         PHP_FALIAS(mysql_fieldtable,    mysql_field_table,      NULL)
     195                 :         PHP_FALIAS(mysql_fieldlen,              mysql_field_len,        NULL)
     196                 :         PHP_FALIAS(mysql_fieldtype,             mysql_field_type,       NULL)
     197                 :         PHP_FALIAS(mysql_fieldflags,    mysql_field_flags,      NULL)
     198                 :         PHP_FALIAS(mysql_selectdb,              mysql_select_db,        NULL)
     199                 : #ifndef NETWARE         /* The below two functions not supported on NetWare */
     200                 : #if MYSQL_VERSION_ID < 40000
     201                 :         PHP_DEP_FALIAS(mysql_createdb,  mysql_create_db,        NULL)
     202                 :         PHP_DEP_FALIAS(mysql_dropdb,    mysql_drop_db,          NULL)
     203                 : #endif
     204                 : #endif  /* NETWARE */
     205                 :         PHP_FALIAS(mysql_freeresult,    mysql_free_result,      NULL)
     206                 :         PHP_FALIAS(mysql_numfields,             mysql_num_fields,       NULL)
     207                 :         PHP_FALIAS(mysql_numrows,               mysql_num_rows,         NULL)
     208                 :         PHP_FALIAS(mysql_listdbs,               mysql_list_dbs,         NULL)
     209                 :         PHP_DEP_FALIAS(mysql_listtables,mysql_list_tables,      NULL)
     210                 :         PHP_FALIAS(mysql_listfields,    mysql_list_fields,      NULL)
     211                 :         PHP_FALIAS(mysql_db_name,               mysql_result,           NULL)
     212                 :         PHP_FALIAS(mysql_dbname,                mysql_result,           NULL)
     213                 :         PHP_FALIAS(mysql_tablename,             mysql_result,           NULL)
     214                 :         PHP_FALIAS(mysql_table_name,    mysql_result,           NULL)
     215                 :         {NULL, NULL, NULL}
     216                 : };
     217                 : /* }}} */
     218                 : 
     219                 : /* {{{ mysql_module_entry
     220                 :  */
     221                 : zend_module_entry mysql_module_entry = {
     222                 :         STANDARD_MODULE_HEADER,
     223                 :         "mysql",
     224                 :         mysql_functions,
     225                 :         ZEND_MODULE_STARTUP_N(mysql),
     226                 :         PHP_MSHUTDOWN(mysql),
     227                 :         PHP_RINIT(mysql),
     228                 :         PHP_RSHUTDOWN(mysql),
     229                 :         PHP_MINFO(mysql),
     230                 :         "1.0",
     231                 :         PHP_MODULE_GLOBALS(mysql),
     232                 :         PHP_GINIT(mysql),
     233                 :         NULL,
     234                 :         NULL,
     235                 :         STANDARD_MODULE_PROPERTIES_EX
     236                 : };
     237                 : /* }}} */
     238                 : 
     239                 : #ifdef COMPILE_DL_MYSQL
     240                 : ZEND_GET_MODULE(mysql)
     241                 : #endif
     242                 : 
     243                 : void timeout(int sig);
     244                 : 
     245                 : #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; } }
     246                 : 
     247                 : #define PHPMY_UNBUFFERED_QUERY_CHECK()                  \
     248                 : {                                                       \
     249                 :         if (mysql->active_result_id) {                       \
     250                 :                 do {                                    \
     251                 :                         int type;                       \
     252                 :                         MYSQL_RES *mysql_result;        \
     253                 :                                                         \
     254                 :                         mysql_result = (MYSQL_RES *) zend_list_find(mysql->active_result_id, &type);     \
     255                 :                         if (mysql_result && type==le_result) {                                          \
     256                 :                                 if (!mysql_eof(mysql_result)) {                                         \
     257                 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Function called without first fetching all rows from a previous unbuffered query");       \
     258                 :                                         while (mysql_fetch_row(mysql_result));  \
     259                 :                                 }                                               \
     260                 :                                 zend_list_delete(mysql->active_result_id);   \
     261                 :                                 mysql->active_result_id = 0;                 \
     262                 :                         }                                                       \
     263                 :                 } while(0);                                                     \
     264                 :         }                                                                       \
     265                 : }                                                                               \
     266                 : 
     267                 : /* {{{ _free_mysql_result
     268                 :  * This wrapper is required since mysql_free_result() returns an integer, and
     269                 :  * thus, cannot be used directly
     270                 :  */
     271                 : static void _free_mysql_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     272               3 : {
     273               3 :         MYSQL_RES *mysql_result = (MYSQL_RES *)rsrc->ptr;
     274                 : 
     275               3 :         mysql_free_result(mysql_result);
     276               3 :         MySG(result_allocated)--;
     277               3 : }
     278                 : /* }}} */
     279                 : 
     280                 : /* {{{ php_mysql_set_default_link
     281                 :  */
     282                 : static void php_mysql_set_default_link(int id TSRMLS_DC)
     283               7 : {
     284               7 :         if (MySG(default_link) != -1) {
     285               0 :                 zend_list_delete(MySG(default_link));
     286                 :         }
     287               7 :         MySG(default_link) = id;
     288               7 :         zend_list_addref(id);
     289               7 : }
     290                 : /* }}} */
     291                 : 
     292                 : /* {{{ php_mysql_select_db
     293                 : */
     294                 : static int php_mysql_select_db(php_mysql_conn *mysql, char *db TSRMLS_DC)
     295               2 : {
     296               2 :         PHPMY_UNBUFFERED_QUERY_CHECK();
     297                 : 
     298               2 :         if (mysql_select_db(&mysql->conn, db) != 0) {
     299               0 :                 return 0;
     300                 :         } else {
     301               2 :                 return 1;
     302                 :         }
     303                 : }
     304                 : /* }}} */
     305                 : 
     306                 : /* {{{ _close_mysql_link
     307                 :  */
     308                 : static void _close_mysql_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     309               7 : {
     310               7 :         php_mysql_conn *link = (php_mysql_conn *)rsrc->ptr;
     311                 :         void (*handler) (int); 
     312                 : 
     313               7 :         handler = signal(SIGPIPE, SIG_IGN);
     314               7 :         mysql_close(&link->conn);
     315               7 :         signal(SIGPIPE, handler);
     316               7 :         efree(link);
     317               7 :         MySG(num_links)--;
     318               7 : }
     319                 : /* }}} */
     320                 : 
     321                 : /* {{{ _close_mysql_plink
     322                 :  */
     323                 : static void _close_mysql_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     324               0 : {
     325               0 :         php_mysql_conn *link = (php_mysql_conn *)rsrc->ptr;
     326                 :         void (*handler) (int);
     327                 : 
     328               0 :         handler = signal(SIGPIPE, SIG_IGN);
     329               0 :         mysql_close(&link->conn);
     330               0 :         signal(SIGPIPE, handler);
     331                 : 
     332               0 :         free(link);
     333               0 :         MySG(num_persistent)--;
     334               0 :         MySG(num_links)--;
     335               0 : }
     336                 : /* }}} */
     337                 : 
     338                 : /* {{{ PHP_INI_MH
     339                 :  */
     340                 : static PHP_INI_MH(OnMySQLPort)
     341            9669 : {
     342            9669 :         if (new_value != NULL) { /* default port */
     343               0 :                 MySG(default_port) = atoi(new_value);
     344                 :         } else {
     345            9669 :                 MySG(default_port) = -1;
     346                 :         }
     347                 : 
     348            9669 :         return SUCCESS;
     349                 : }
     350                 : /* }}} */
     351                 : 
     352                 : /* {{{ PHP_INI */
     353                 : PHP_INI_BEGIN()
     354                 :         STD_PHP_INI_BOOLEAN("mysql.allow_persistent", "1",  PHP_INI_SYSTEM,         OnUpdateLong,           allow_persistent,       zend_mysql_globals,             mysql_globals)
     355                 :         STD_PHP_INI_ENTRY_EX("mysql.max_persistent",  "-1", PHP_INI_SYSTEM,         OnUpdateLong,           max_persistent,         zend_mysql_globals,             mysql_globals,  display_link_numbers)
     356                 :         STD_PHP_INI_ENTRY_EX("mysql.max_links",                       "-1", PHP_INI_SYSTEM,         OnUpdateLong,           max_links,                      zend_mysql_globals,             mysql_globals,  display_link_numbers)
     357                 :         STD_PHP_INI_ENTRY("mysql.default_host",                       NULL,   PHP_INI_ALL,            OnUpdateString,         default_host,           zend_mysql_globals,             mysql_globals)
     358                 :         STD_PHP_INI_ENTRY("mysql.default_user",                       NULL,   PHP_INI_ALL,            OnUpdateString,         default_user,           zend_mysql_globals,             mysql_globals)
     359                 :         STD_PHP_INI_ENTRY("mysql.default_password",           NULL,   PHP_INI_ALL,            OnUpdateString,         default_password,       zend_mysql_globals,             mysql_globals)
     360                 :         PHP_INI_ENTRY("mysql.default_port",                           NULL,   PHP_INI_ALL,            OnMySQLPort)
     361                 :         STD_PHP_INI_ENTRY("mysql.default_socket",             NULL,   PHP_INI_ALL,            OnUpdateStringUnempty,  default_socket, zend_mysql_globals,             mysql_globals)
     362                 :         STD_PHP_INI_ENTRY("mysql.connect_timeout",            "60", PHP_INI_ALL,            OnUpdateLong,           connect_timeout,        zend_mysql_globals,             mysql_globals)
     363                 :         STD_PHP_INI_BOOLEAN("mysql.trace_mode",                       "0",  PHP_INI_ALL,            OnUpdateLong,           trace_mode,             zend_mysql_globals,             mysql_globals)
     364                 : PHP_INI_END()
     365                 : /* }}} */
     366                 : 
     367                 : /* {{{ PHP_GINIT_FUNCTION
     368                 :  */
     369                 : static PHP_GINIT_FUNCTION(mysql)
     370            9669 : {
     371            9669 :         mysql_globals->num_persistent = 0;
     372            9669 :         mysql_globals->default_socket = NULL;
     373            9669 :         mysql_globals->default_host = NULL;
     374            9669 :         mysql_globals->default_user = NULL;
     375            9669 :         mysql_globals->default_password = NULL;
     376            9669 :         mysql_globals->connect_errno = 0;
     377            9669 :         mysql_globals->connect_error = NULL;
     378            9669 :         mysql_globals->connect_timeout = 0;
     379            9669 :         mysql_globals->trace_mode = 0;
     380            9669 :         mysql_globals->result_allocated = 0;
     381            9669 : }
     382                 : /* }}} */
     383                 : 
     384                 : /* {{{ PHP_MINIT_FUNCTION
     385                 :  */
     386                 : ZEND_MODULE_STARTUP_D(mysql)
     387            9669 : {
     388            9669 :         REGISTER_INI_ENTRIES();
     389            9669 :         le_result = zend_register_list_destructors_ex(_free_mysql_result, NULL, "mysql result", module_number);
     390            9669 :         le_link = zend_register_list_destructors_ex(_close_mysql_link, NULL, "mysql link", module_number);
     391            9669 :         le_plink = zend_register_list_destructors_ex(NULL, _close_mysql_plink, "mysql link persistent", module_number);
     392            9669 :         Z_TYPE(mysql_module_entry) = type;
     393                 : 
     394            9669 :         REGISTER_LONG_CONSTANT("MYSQL_ASSOC", MYSQL_ASSOC, CONST_CS | CONST_PERSISTENT);
     395            9669 :         REGISTER_LONG_CONSTANT("MYSQL_NUM", MYSQL_NUM, CONST_CS | CONST_PERSISTENT);
     396            9669 :         REGISTER_LONG_CONSTANT("MYSQL_BOTH", MYSQL_BOTH, CONST_CS | CONST_PERSISTENT);
     397            9669 :         REGISTER_LONG_CONSTANT("MYSQL_CLIENT_COMPRESS", CLIENT_COMPRESS, CONST_CS | CONST_PERSISTENT);
     398                 : #if MYSQL_VERSION_ID >= 40000        
     399            9669 :         REGISTER_LONG_CONSTANT("MYSQL_CLIENT_SSL", CLIENT_SSL, CONST_CS | CONST_PERSISTENT);
     400                 : #endif
     401            9669 :         REGISTER_LONG_CONSTANT("MYSQL_CLIENT_INTERACTIVE", CLIENT_INTERACTIVE, CONST_CS | CONST_PERSISTENT);
     402            9669 :         REGISTER_LONG_CONSTANT("MYSQL_CLIENT_IGNORE_SPACE", CLIENT_IGNORE_SPACE, CONST_CS | CONST_PERSISTENT); 
     403                 : 
     404                 : #if MYSQL_VERSION_ID >= 40000
     405            9669 :         if (mysql_server_init(0, NULL, NULL)) {
     406               0 :                 return FAILURE;
     407                 :         }
     408                 : #endif
     409                 : 
     410            9669 :         return SUCCESS;
     411                 : }
     412                 : /* }}} */
     413                 : 
     414                 : /* {{{ PHP_MSHUTDOWN_FUNCTION
     415                 :  */
     416                 : PHP_MSHUTDOWN_FUNCTION(mysql)
     417            9681 : {
     418                 : #if MYSQL_VERSION_ID >= 40000
     419                 : #ifdef PHP_WIN32
     420                 :         unsigned long client_ver = mysql_get_client_version();
     421                 :         /* Can't call mysql_server_end() multiple times prior to 5.0.42 on Windows */
     422                 :         if ((client_ver >= 50046 && client_ver < 50100) || client_ver > 50122) {
     423                 :                 mysql_server_end();
     424                 :         }
     425                 : #else
     426            9681 :         mysql_server_end();
     427                 : #endif
     428                 : #endif
     429                 : 
     430            9681 :         UNREGISTER_INI_ENTRIES();
     431            9681 :         return SUCCESS;
     432                 : }
     433                 : /* }}} */
     434                 : 
     435                 : /* {{{ PHP_RINIT_FUNCTION
     436                 :  */
     437                 : PHP_RINIT_FUNCTION(mysql)
     438            9655 : {
     439                 : #if defined(ZTS) && MYSQL_VERSION_ID >= 40000
     440                 :         if (mysql_thread_init()) {
     441                 :                 return FAILURE;
     442                 :         }
     443                 : #endif
     444            9655 :         MySG(default_link)=-1;
     445            9655 :         MySG(num_links) = MySG(num_persistent);
     446                 :         /* Reset connect error/errno on every request */
     447            9655 :         MySG(connect_error) = NULL;
     448            9655 :         MySG(connect_errno) =0;
     449            9655 :         MySG(result_allocated) = 0;
     450                 : 
     451            9655 :         return SUCCESS;
     452                 : }
     453                 : /* }}} */
     454                 : 
     455                 : /* {{{ PHP_RSHUTDOWN_FUNCTION
     456                 :  */
     457                 : PHP_RSHUTDOWN_FUNCTION(mysql)
     458            9667 : {
     459                 : #if defined(ZTS) && MYSQL_VERSION_ID >= 40000
     460                 :         mysql_thread_end();
     461                 : #endif
     462                 : 
     463            9667 :         if (MySG(trace_mode)) {
     464               0 :                 if (MySG(result_allocated)){
     465               0 :                         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));
     466                 :                 }
     467                 :         }
     468                 : 
     469            9667 :         if (MySG(connect_error)!=NULL) {
     470               0 :                 efree(MySG(connect_error));
     471                 :         }
     472            9667 :         return SUCCESS;
     473                 : }
     474                 : /* }}} */
     475                 : 
     476                 : /* {{{ PHP_MINFO_FUNCTION
     477                 :  */
     478                 : PHP_MINFO_FUNCTION(mysql)
     479               5 : {
     480                 :         char buf[32];
     481                 : 
     482               5 :         php_info_print_table_start();
     483               5 :         php_info_print_table_header(2, "MySQL Support", "enabled");
     484               5 :         snprintf(buf, sizeof(buf), "%ld", MySG(num_persistent));
     485               5 :         php_info_print_table_row(2, "Active Persistent Links", buf);
     486               5 :         snprintf(buf, sizeof(buf), "%ld", MySG(num_links));
     487               5 :         php_info_print_table_row(2, "Active Links", buf);
     488               5 :         php_info_print_table_row(2, "Client API version", mysql_get_client_info());
     489                 : #if !defined (PHP_WIN32) && !defined (NETWARE)
     490               5 :         php_info_print_table_row(2, "MYSQL_MODULE_TYPE", PHP_MYSQL_TYPE);
     491               5 :         php_info_print_table_row(2, "MYSQL_SOCKET", MYSQL_UNIX_ADDR);
     492               5 :         php_info_print_table_row(2, "MYSQL_INCLUDE", PHP_MYSQL_INCLUDE);
     493               5 :         php_info_print_table_row(2, "MYSQL_LIBS", PHP_MYSQL_LIBS);
     494                 : #endif
     495                 : 
     496               5 :         php_info_print_table_end();
     497                 : 
     498               5 :         DISPLAY_INI_ENTRIES();
     499                 : 
     500               5 : }
     501                 : /* }}} */
     502                 : 
     503                 : /* {{{ php_mysql_do_connect
     504                 :  */
     505                 : #define MYSQL_DO_CONNECT_CLEANUP()      \
     506                 :         if (free_host) {                                \
     507                 :                 efree(host);                            \
     508                 :         }
     509                 : 
     510                 : #define MYSQL_DO_CONNECT_RETURN_FALSE()         \
     511                 :         MYSQL_DO_CONNECT_CLEANUP();                             \
     512                 :         RETURN_FALSE;
     513                 : 
     514                 : static void php_mysql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
     515               7 : {
     516               7 :         char *user=NULL, *passwd=NULL, *host_and_port=NULL, *socket=NULL, *tmp=NULL, *host=NULL;
     517                 :         int  user_len, passwd_len, host_len;
     518               7 :         char *hashed_details=NULL;
     519               7 :         int hashed_details_length, port = MYSQL_PORT;
     520               7 :         long client_flags = 0;
     521               7 :         php_mysql_conn *mysql=NULL;
     522                 : #if MYSQL_VERSION_ID <= 32230
     523                 :         void (*handler) (int);
     524                 : #endif
     525               7 :         zend_bool free_host=0, new_link=0;
     526                 :         long connect_timeout;
     527                 : 
     528                 : 
     529               7 :         connect_timeout = MySG(connect_timeout);
     530                 : 
     531               7 :         socket = MySG(default_socket);
     532                 : 
     533               7 :         if (MySG(default_port) < 0) {
     534                 : #if !defined(PHP_WIN32) && !defined(NETWARE)
     535                 :                 struct servent *serv_ptr;
     536                 :                 char *env;
     537                 :                 
     538               6 :                 MySG(default_port) = MYSQL_PORT;
     539               6 :                 if ((serv_ptr = getservbyname("mysql", "tcp"))) {
     540               6 :                         MySG(default_port) = (uint) ntohs((ushort) serv_ptr->s_port);
     541                 :                 }
     542               6 :                 if ((env = getenv("MYSQL_TCP_PORT"))) {
     543               0 :                         MySG(default_port) = (uint) atoi(env);
     544                 :                 }
     545                 : #else
     546                 :                 MySG(default_port) = MYSQL_PORT;
     547                 : #endif
     548                 :         }
     549                 :         
     550               7 :         if (PG(sql_safe_mode)) {
     551               0 :                 if (ZEND_NUM_ARGS()>0) {
     552               0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "SQL safe mode in effect - ignoring host/user/password information");
     553                 :                 }
     554               0 :                 host_and_port=passwd=NULL;
     555               0 :                 user=php_get_current_user();
     556               0 :                 hashed_details_length = spprintf(&hashed_details, 0, "mysql__%s_", user);
     557               0 :                 client_flags = CLIENT_INTERACTIVE;
     558                 :         } else {
     559                 :                 /* mysql_pconnect does not support new_link parameter */
     560               7 :                 if (persistent) {
     561               0 :                         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!s!l", &host_and_port, &host_len,
     562                 :                                                                         &user, &user_len, &passwd, &passwd_len,
     563                 :                                                                         &client_flags)==FAILURE) {
     564               0 :                                 return;
     565                 :                 }
     566                 :                 } else {
     567               7 :                         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!s!bl", &host_and_port, &host_len,
     568                 :                                                                                 &user, &user_len, &passwd, &passwd_len, 
     569                 :                                                                                 &new_link, &client_flags)==FAILURE) {
     570               0 :                                 return;
     571                 :                         }
     572                 :                 }
     573                 : 
     574               7 :                 if (!host_and_port) {
     575               0 :                         host_and_port = MySG(default_host);
     576                 :                 }
     577               7 :                 if (!user) {
     578               0 :                         user = MySG(default_user);
     579                 :                 }
     580               7 :                 if (!passwd) {
     581               0 :                         passwd = MySG(default_password);
     582                 :                 }
     583                 : 
     584                 :                 /* disable local infile option for open_basedir */
     585               7 :                 if (((PG(open_basedir) && PG(open_basedir)[0] != '\0') || PG(safe_mode)) && (client_flags & CLIENT_LOCAL_FILES)) {
     586               0 :                         client_flags ^= CLIENT_LOCAL_FILES;
     587                 :                 }
     588                 : 
     589               7 :                 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);
     590                 :         }
     591                 : 
     592                 :         /* We cannot use mysql_port anymore in windows, need to use
     593                 :          * mysql_real_connect() to set the port.
     594                 :          */
     595               8 :         if (host_and_port && (tmp=strchr(host_and_port, ':'))) {
     596               1 :                 host = estrndup(host_and_port, tmp-host_and_port);
     597               1 :                 free_host = 1;
     598               1 :                 tmp++;
     599               1 :                 if (tmp[0] != '/') {
     600               1 :                         port = atoi(tmp);
     601               1 :                         if ((tmp=strchr(tmp, ':'))) {
     602               0 :                                 tmp++;
     603               0 :                                 socket=tmp;
     604                 :                         }
     605                 :                 } else {
     606               0 :                         socket = tmp;
     607                 :                 }
     608                 :         } else {
     609               6 :                 host = host_and_port;
     610               6 :                 port = MySG(default_port);
     611                 :         }
     612                 : 
     613                 : #if MYSQL_VERSION_ID < 32200
     614                 :         mysql_port = port;
     615                 : #endif
     616                 : 
     617               7 :         if (!MySG(allow_persistent)) {
     618               0 :                 persistent=0;
     619                 :         }
     620               7 :         if (persistent) {
     621                 :                 zend_rsrc_list_entry *le;
     622                 : 
     623                 :                 /* try to find if we already have this link in our persistent list */
     624               0 :                 if (zend_hash_find(&EG(persistent_list), hashed_details, hashed_details_length+1, (void **) &le)==FAILURE) {  /* we don't */
     625                 :                         zend_rsrc_list_entry new_le;
     626                 : 
     627               0 :                         if (MySG(max_links)!=-1 && MySG(num_links)>=MySG(max_links)) {
     628               0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%ld)", MySG(num_links));
     629               0 :                                 efree(hashed_details);
     630               0 :                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     631                 :                         }
     632               0 :                         if (MySG(max_persistent)!=-1 && MySG(num_persistent)>=MySG(max_persistent)) {
     633               0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open persistent links (%ld)", MySG(num_persistent));
     634               0 :                                 efree(hashed_details);
     635               0 :                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     636                 :                         }
     637                 :                         /* create the link */
     638               0 :                         mysql = (php_mysql_conn *) malloc(sizeof(php_mysql_conn));
     639               0 :                         mysql->active_result_id = 0;
     640                 : #if MYSQL_VERSION_ID > 32199 /* this lets us set the port number */
     641               0 :                         mysql_init(&mysql->conn);
     642                 : 
     643               0 :                         if (connect_timeout != -1) {
     644               0 :                                 mysql_options(&mysql->conn, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout);
     645                 :                         }
     646                 : 
     647               0 :                         if (mysql_real_connect(&mysql->conn, host, user, passwd, NULL, port, socket, client_flags)==NULL) {
     648                 : #else
     649                 :                         if (mysql_connect(&mysql->conn, host, user, passwd)==NULL) {
     650                 : #endif
     651                 :                                 /* Populate connect error globals so that the error functions can read them */
     652               0 :                                 if (MySG(connect_error)!=NULL) efree(MySG(connect_error));
     653               0 :                                 MySG(connect_error)=estrdup(mysql_error(&mysql->conn));
     654               0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", MySG(connect_error));
     655                 : #if defined(HAVE_MYSQL_ERRNO)
     656               0 :                                 MySG(connect_errno)=mysql_errno(&mysql->conn);
     657                 : #endif
     658               0 :                                 free(mysql);
     659               0 :                                 efree(hashed_details);
     660               0 :                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     661                 :                         }
     662                 : 
     663                 :                         /* hash it up */
     664               0 :                         Z_TYPE(new_le) = le_plink;
     665               0 :                         new_le.ptr = mysql;
     666               0 :                         if (zend_hash_update(&EG(persistent_list), hashed_details, hashed_details_length+1, (void *) &new_le, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
     667               0 :                                 free(mysql);
     668               0 :                                 efree(hashed_details);
     669               0 :                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     670                 :                         }
     671               0 :                         MySG(num_persistent)++;
     672               0 :                         MySG(num_links)++;
     673                 :                 } else {  /* The link is in our list of persistent connections */
     674               0 :                         if (Z_TYPE_P(le) != le_plink) {
     675               0 :                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     676                 :                         }
     677                 :                         /* ensure that the link did not die */
     678                 : #if MYSQL_VERSION_ID > 32230 /* Use mysql_ping to ensure link is alive (and to reconnect if needed) */
     679               0 :                         if (mysql_ping(le->ptr)) {
     680                 : #else   /* Use mysql_stat() to check if server is alive */
     681                 :                         handler=signal(SIGPIPE, SIG_IGN);
     682                 : #if defined(HAVE_MYSQL_ERRNO) && defined(CR_SERVER_GONE_ERROR)
     683                 :                         mysql_stat(le->ptr);
     684                 :                         if (mysql_errno(&((php_mysql_conn *) le->ptr)->conn) == CR_SERVER_GONE_ERROR) {
     685                 : #else
     686                 :                         if (!strcasecmp(mysql_stat(le->ptr), "mysql server has gone away")) { /* the link died */
     687                 : #endif
     688                 :                                 signal(SIGPIPE, handler);
     689                 : #endif /* end mysql_ping */
     690                 : #if MYSQL_VERSION_ID > 32199 /* this lets us set the port number */
     691               0 :                                 if (mysql_real_connect(le->ptr, host, user, passwd, NULL, port, socket, client_flags)==NULL) {
     692                 : #else
     693                 :                                 if (mysql_connect(le->ptr, host, user, passwd)==NULL) {
     694                 : #endif
     695               0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Link to server lost, unable to reconnect");
     696               0 :                                         zend_hash_del(&EG(persistent_list), hashed_details, hashed_details_length+1);
     697               0 :                                         efree(hashed_details);
     698               0 :                                         MYSQL_DO_CONNECT_RETURN_FALSE();
     699                 :                                 }
     700                 :                         }
     701                 : #if MYSQL_VERSION_ID < 32231
     702                 :                         signal(SIGPIPE, handler);
     703                 : #endif
     704                 : 
     705               0 :                         mysql = (php_mysql_conn *) le->ptr;
     706               0 :                         mysql->active_result_id = 0;
     707                 :                 }
     708               0 :                 ZEND_REGISTER_RESOURCE(return_value, mysql, le_plink);
     709                 :         } else { /* non persistent */
     710                 :                 zend_rsrc_list_entry *index_ptr, new_index_ptr;
     711                 :                 
     712                 :                 /* first we check the hash for the hashed_details key.  if it exists,
     713                 :                  * it should point us to the right offset where the actual mysql link sits.
     714                 :                  * if it doesn't, open a new mysql link, add it to the resource list,
     715                 :                  * and add a pointer to it with hashed_details as the key.
     716                 :                  */
     717               7 :                 if (!new_link && zend_hash_find(&EG(regular_list), hashed_details, hashed_details_length+1,(void **) &index_ptr)==SUCCESS) {
     718                 :                         int type;
     719                 :                         long link;
     720                 :                         void *ptr;
     721                 : 
     722               0 :                         if (Z_TYPE_P(index_ptr) != le_index_ptr) {
     723               0 :                                 MYSQL_DO_CONNECT_RETURN_FALSE();
     724                 :                         }
     725               0 :                         link = (long) index_ptr->ptr;
     726               0 :                         ptr = zend_list_find(link,&type);   /* check if the link is still there */
     727               0 :                         if (ptr && (type==le_link || type==le_plink)) {
     728               0 :                                 zend_list_addref(link);
     729               0 :                                 Z_LVAL_P(return_value) = link;
     730               0 :                                 php_mysql_set_default_link(link TSRMLS_CC);
     731               0 :                                 Z_TYPE_P(return_value) = IS_RESOURCE;
     732               0 :                                 efree(hashed_details);
     733               0 :                                 MYSQL_DO_CONNECT_CLEANUP();
     734               0 :                                 return;
     735                 :                         } else {
     736               0 :                                 zend_hash_del(&EG(regular_list), hashed_details, hashed_details_length+1);
     737                 :                         }
     738                 :                 }
     739               7 :                 if (MySG(max_links)!=-1 && MySG(num_links)>=MySG(max_links)) {
     740               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%ld)", MySG(num_links));
     741               0 :                         efree(hashed_details);
     742               0 :                         MYSQL_DO_CONNECT_RETURN_FALSE();
     743                 :                 }
     744                 : 
     745               7 :                 mysql = (php_mysql_conn *) emalloc(sizeof(php_mysql_conn));
     746               7 :                 mysql->active_result_id = 0;
     747                 : #if MYSQL_VERSION_ID > 32199 /* this lets us set the port number */
     748               7 :                 mysql_init(&mysql->conn);
     749                 : 
     750               7 :                 if (connect_timeout != -1) {
     751               7 :                         mysql_options(&mysql->conn, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout);
     752                 :                 }
     753                 : 
     754               7 :                 if (mysql_real_connect(&mysql->conn, host, user, passwd, NULL, port, socket, client_flags)==NULL) {
     755                 : #else
     756                 :                 if (mysql_connect(&mysql->conn, host, user, passwd)==NULL) {
     757                 : #endif
     758                 :                         /* Populate connect error globals so that the error functions can read them */
     759               0 :                         if (MySG(connect_error)!=NULL) efree(MySG(connect_error));
     760               0 :                         MySG(connect_error)=estrdup(mysql_error(&mysql->conn));
     761               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", MySG(connect_error));
     762                 : #if defined(HAVE_MYSQL_ERRNO)
     763               0 :                         MySG(connect_errno)=mysql_errno(&mysql->conn);
     764                 : #endif
     765               0 :                         efree(hashed_details);
     766               0 :                         efree(mysql);
     767               0 :                         MYSQL_DO_CONNECT_RETURN_FALSE();
     768                 :                 }
     769                 : 
     770                 :                 /* add it to the list */
     771               7 :                 ZEND_REGISTER_RESOURCE(return_value, mysql, le_link);
     772                 : 
     773                 :                 /* add it to the hash */
     774               7 :                 new_index_ptr.ptr = (void *) Z_LVAL_P(return_value);
     775               7 :                 Z_TYPE(new_index_ptr) = le_index_ptr;
     776               7 :                 if (zend_hash_update(&EG(regular_list), hashed_details, hashed_details_length+1,(void *) &new_index_ptr, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
     777               0 :                         efree(hashed_details);
     778               0 :                         MYSQL_DO_CONNECT_RETURN_FALSE();
     779                 :                 }
     780               7 :                 MySG(num_links)++;
     781                 :         }
     782                 : 
     783               7 :         efree(hashed_details);
     784               7 :         php_mysql_set_default_link(Z_LVAL_P(return_value) TSRMLS_CC);
     785               7 :         MYSQL_DO_CONNECT_CLEANUP();
     786                 : }
     787                 : /* }}} */
     788                 : 
     789                 : /* {{{ php_mysql_get_default_link
     790                 :  */
     791                 : static int php_mysql_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
     792              13 : {
     793              13 :         if (MySG(default_link)==-1) { /* no link opened yet, implicitly open one */
     794               0 :                 ht = 0;
     795               0 :                 php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
     796                 :         }
     797              13 :         return MySG(default_link);
     798                 : }
     799                 : /* }}} */
     800                 : 
     801                 : /* {{{ proto resource mysql_connect([string hostname[:port][:/path/to/socket] [, string username [, string password [, bool new [, int flags]]]]])
     802                 :    Opens a connection to a MySQL Server */
     803                 : PHP_FUNCTION(mysql_connect)
     804               7 : {
     805               7 :         php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
     806               7 : }
     807                 : /* }}} */
     808                 : 
     809                 : /* {{{ proto resource mysql_pconnect([string hostname[:port][:/path/to/socket] [, string username [, string password [, int flags]]]])
     810                 :    Opens a persistent connection to a MySQL Server */
     811                 : PHP_FUNCTION(mysql_pconnect)
     812               0 : {
     813               0 :         php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
     814               0 : }
     815                 : /* }}} */
     816                 : 
     817                 : /* {{{ proto bool mysql_close([int link_identifier])
     818                 :    Close a MySQL connection */
     819                 : PHP_FUNCTION(mysql_close)
     820               7 : {
     821               7 :         zval **mysql_link=NULL;
     822                 :         int id;
     823                 :         php_mysql_conn *mysql;
     824                 : 
     825               7 :         switch (ZEND_NUM_ARGS()) {
     826                 :                 case 0:
     827               0 :                         id = MySG(default_link);
     828               0 :                         break;
     829                 :                 case 1:
     830               7 :                         if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
     831               0 :                                 RETURN_FALSE;
     832                 :                         }
     833               7 :                         id = -1;
     834               7 :                         break;
     835                 :                 default:
     836               0 :                         WRONG_PARAM_COUNT;
     837                 :                         break;
     838                 :         }
     839                 :         
     840               7 :         ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
     841                 : 
     842               7 :         if (id==-1) { /* explicit resource number */
     843               7 :                 PHPMY_UNBUFFERED_QUERY_CHECK();
     844               7 :                 zend_list_delete(Z_RESVAL_PP(mysql_link));
     845                 :         }
     846                 : 
     847               7 :         if (id!=-1 
     848                 :                 || (mysql_link && Z_RESVAL_PP(mysql_link)==MySG(default_link))) {
     849               7 :                 PHPMY_UNBUFFERED_QUERY_CHECK();
     850               7 :                 zend_list_delete(MySG(default_link));
     851               7 :                 MySG(default_link) = -1;
     852                 :         }
     853                 : 
     854               7 :         RETURN_TRUE;
     855                 : }
     856                 : /* }}} */
     857                 : 
     858                 : /* {{{ proto bool mysql_select_db(string database_name [, int link_identifier])
     859                 :    Selects a MySQL database */
     860                 : PHP_FUNCTION(mysql_select_db)
     861               2 : {
     862                 :         zval **db, **mysql_link;
     863                 :         int id;
     864                 :         php_mysql_conn *mysql;
     865                 :         
     866               2 :         switch(ZEND_NUM_ARGS()) {
     867                 :                 case 1:
     868               2 :                         if (zend_get_parameters_ex(1, &db)==FAILURE) {
     869               0 :                                 RETURN_FALSE;
     870                 :                         }
     871               2 :                         id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     872               2 :                         CHECK_LINK(id);
     873               2 :                         break;
     874                 :                 case 2:
     875               0 :                         if (zend_get_parameters_ex(2, &db, &mysql_link)==FAILURE) {
     876               0 :                                 RETURN_FALSE;
     877                 :                         }
     878               0 :                         id = -1;
     879               0 :                         break;
     880                 :                 default:
     881               0 :                         WRONG_PARAM_COUNT;
     882                 :                         break;
     883                 :         }
     884                 : 
     885               2 :         ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
     886                 :         
     887               2 :         convert_to_string_ex(db);
     888                 : 
     889               2 :         if (php_mysql_select_db(mysql, Z_STRVAL_PP(db) TSRMLS_CC)) {
     890               2 :                 RETURN_TRUE;
     891                 :         } else {
     892               0 :                 RETURN_FALSE;   
     893                 :         }
     894                 : }
     895                 : /* }}} */
     896                 : 
     897                 : #ifdef HAVE_GETINFO_FUNCS
     898                 : 
     899                 : /* {{{ proto string mysql_get_client_info(void)
     900                 :    Returns a string that represents the client library version */
     901                 : PHP_FUNCTION(mysql_get_client_info)
     902               0 : {
     903               0 :         if (ZEND_NUM_ARGS() != 0) {
     904               0 :                 WRONG_PARAM_COUNT;
     905                 :         }
     906                 : 
     907               0 :         RETURN_STRING((char *)mysql_get_client_info(),1);       
     908                 : }
     909                 : /* }}} */
     910                 : 
     911                 : /* {{{ proto string mysql_get_host_info([int link_identifier])
     912                 :    Returns a string describing the type of connection in use, including the server host name */
     913                 : PHP_FUNCTION(mysql_get_host_info)
     914               0 : {
     915                 :         zval **mysql_link;
     916                 :         int id;
     917                 :         php_mysql_conn *mysql;
     918                 : 
     919               0 :         switch(ZEND_NUM_ARGS()) {
     920                 :                 case 0:
     921               0 :                         id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     922               0 :                         CHECK_LINK(id);
     923               0 :                         break;
     924                 :                 case 1:
     925               0 :                         if (zend_get_parameters_ex(1,&mysql_link)==FAILURE) {
     926               0 :                                 RETURN_FALSE;
     927                 :                         }
     928               0 :                         id = -1;
     929               0 :                         break;
     930                 :                 default:
     931               0 :                         WRONG_PARAM_COUNT;
     932                 :                         break;
     933                 :         }
     934                 : 
     935               0 :         ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
     936                 : 
     937               0 :         RETURN_STRING((char *)mysql_get_host_info(&mysql->conn),1);
     938                 : }
     939                 : /* }}} */
     940                 : 
     941                 : /* {{{ proto int mysql_get_proto_info([int link_identifier])
     942                 :    Returns the protocol version used by current connection */
     943                 : PHP_FUNCTION(mysql_get_proto_info)
     944               0 : {
     945                 :         zval **mysql_link;
     946                 :         int id;
     947                 :         php_mysql_conn *mysql;
     948                 : 
     949               0 :         switch(ZEND_NUM_ARGS()) {
     950                 :                 case 0:
     951               0 :                         id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     952               0 :                         CHECK_LINK(id);
     953               0 :                         break;
     954                 :                 case 1:
     955               0 :                         if (zend_get_parameters_ex(1,&mysql_link)==FAILURE) {
     956               0 :                                 RETURN_FALSE;
     957                 :                         }
     958               0 :                         id = -1;
     959               0 :                         break;
     960                 :                 default:
     961               0 :                         WRONG_PARAM_COUNT;
     962                 :                         break;
     963                 :         }
     964                 : 
     965               0 :         ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
     966                 : 
     967               0 :         RETURN_LONG(mysql_get_proto_info(&mysql->conn));
     968                 : }
     969                 : /* }}} */
     970                 : 
     971                 : /* {{{ proto string mysql_get_server_info([int link_identifier])
     972                 :    Returns a string that represents the server version number */
     973                 : PHP_FUNCTION(mysql_get_server_info)
     974               0 : {
     975                 :         zval **mysql_link;
     976                 :         int id;
     977                 :         php_mysql_conn *mysql;
     978                 : 
     979               0 :         switch(ZEND_NUM_ARGS()) {
     980                 :                 case 0:
     981               0 :                         id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     982               0 :                         CHECK_LINK(id);
     983               0 :                         break;
     984                 :                 case 1:
     985               0 :                         if (zend_get_parameters_ex(1,&mysql_link)==FAILURE) {
     986               0 :                                 RETURN_FALSE;
     987                 :                         }
     988               0 :                         id = -1;
     989               0 :                         break;
     990                 :                 default:
     991               0 :                         WRONG_PARAM_COUNT;
     992                 :                         break;
     993                 :         }
     994                 : 
     995               0 :         ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
     996                 : 
     997               0 :         RETURN_STRING((char *)mysql_get_server_info(&mysql->conn),1);
     998                 : }
     999                 : /* }}} */
    1000                 : 
    1001                 : /* {{{ proto string mysql_info([int link_identifier])
    1002                 :    Returns a string containing information about the most recent query */
    1003                 : PHP_FUNCTION(mysql_info)
    1004               0 : {
    1005                 :         zval *mysql_link;
    1006               0 :         int id = -1;
    1007                 :         char *str;
    1008                 :         php_mysql_conn *mysql;
    1009                 : 
    1010               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1011               0 :                 return;
    1012                 :         }
    1013                 : 
    1014               0 :         if (ZEND_NUM_ARGS() == 0) {
    1015               0 :                 id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1016               0 :                 CHECK_LINK(id);
    1017                 :         }
    1018                 : 
    1019               0 :         ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
    1020                 : 
    1021               0 :         if ((str = (char *)mysql_info(&mysql->conn))) {
    1022               0 :                 RETURN_STRING(str,1);
    1023                 :         } else {
    1024               0 :                 RETURN_FALSE;
    1025                 :         }
    1026                 : }
    1027                 : /* }}} */
    1028                 : 
    1029                 : /* {{{ proto int mysql_thread_id([int link_identifier])
    1030                 :         Returns the thread id of current connection */
    1031                 : PHP_FUNCTION(mysql_thread_id)
    1032               0 : {
    1033               0 :         zval *mysql_link = NULL;
    1034               0 :         int  id = -1;
    1035                 :         php_mysql_conn *mysql;
    1036                 : 
    1037               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1038               0 :                 return;
    1039                 :         }
    1040                 : 
    1041               0 :         if (ZEND_NUM_ARGS() == 0) {
    1042               0 :                 id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1043               0 :                 CHECK_LINK(id);         
    1044                 :         }
    1045               0 :         ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
    1046                 : 
    1047               0 :         RETURN_LONG(mysql_thread_id(&mysql->conn));
    1048                 : }
    1049                 : /* }}} */
    1050                 : 
    1051                 : /* {{{ proto string mysql_stat([int link_identifier])
    1052                 :         Returns a string containing status information */
    1053                 : PHP_FUNCTION(mysql_stat)
    1054               0 : {
    1055               0 :         zval *mysql_link = NULL;
    1056               0 :         int id = -1;
    1057                 :         php_mysql_conn *mysql;
    1058                 : 
    1059               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1060               0 :                 return;
    1061                 :         }
    1062                 : 
    1063               0 :         if (ZEND_NUM_ARGS() == 0) {
    1064               0 :                 id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1065               0 :                 CHECK_LINK(id);         
    1066                 :         }
    1067               0 :         ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
    1068                 : 
    1069               0 :         PHPMY_UNBUFFERED_QUERY_CHECK();
    1070                 : 
    1071               0 :         RETURN_STRING((char *)mysql_stat(&mysql->conn), 1);
    1072                 : }
    1073                 : /* }}} */
    1074                 : 
    1075                 : /* {{{ proto string mysql_client_encoding([int link_identifier])
    1076                 :         Returns the default character set for the current connection */
    1077                 : PHP_FUNCTION(mysql_client_encoding)
    1078               0 : {
    1079               0 :         zval *mysql_link = NULL;
    1080               0 :         int id = -1;
    1081                 :         php_mysql_conn *mysql;
    1082                 : 
    1083               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
    1084               0 :                 return;
    1085                 :         }
    1086                 : 
    1087               0 :         if (ZEND_NUM_ARGS() == 0) {
    1088               0 :                 id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1089               0 :                 CHECK_LINK(id);
    1090                 :         }
    1091                 : 
    1092               0 :         ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
    1093                 : 
    1094               0 :         RETURN_STRING((char *)mysql_character_set_name(&mysql->conn), 1);
    1095                 : }
    1096                 : /* }}} */
    1097                 : #endif
    1098                 : 
    1099                 : #ifdef MYSQL_HAS_SET_CHARSET
    1100                 : /* {{{ proto bool mysql_set_charset(string csname [, int link_identifier])
    1101                 :    sets client character set */
    1102                 : PHP_FUNCTION(mysql_set_charset)
    1103               0 : {
    1104               0 :         zval *mysql_link = NULL;
    1105                 :         char *csname;
    1106               0 :         int id = -1, csname_len;
    1107                 :         php_mysql_conn *mysql;
    1108                 : 
    1109               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &csname, &csname_len, &mysql_link) == FAILURE) {
    1110               0 :                 return;
    1111                 :         }
    1112                 : 
    1113               0 :         if (ZEND_NUM_ARGS() == 1) {
    1114               0 :                 id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1115               0 :                 CHECK_LINK(id);
    1116                 :         }
    1117                 : 
    1118               0 :         ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
    1119                 : 
    1120               0 :         if (!mysql_set_character_set(&mysql->conn, csname)) {
    1121               0 :                 RETURN_TRUE;
    1122                 :         } else {
    1123               0 :                 RETURN_FALSE;
    1124                 :         }
    1125                 : }
    1126                 : /* }}} */
    1127                 : #endif
    1128                 : 
    1129                 : #ifndef NETWARE         /* The below two functions not supported on NetWare */
    1130                 : #if MYSQL_VERSION_ID < 40000
    1131                 : /* {{{ proto bool mysql_create_db(string database_name [, int link_identifier])
    1132                 :    Create a MySQL database */
    1133                 : PHP_FUNCTION(mysql_create_db)
    1134                 : {
    1135                 :         zval **db, **mysql_link;
    1136                 :         int id;
    1137                 :         php_mysql_conn *mysql;
    1138                 : 
    1139                 :         switch(ZEND_NUM_ARGS()) {
    1140                 :                 case 1:
    1141                 :                         if (zend_get_parameters_ex(1, &db)==FAILURE) {
    1142                 :                                 RETURN_FALSE;
    1143                 :                         }
    1144                 :                         id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1145                 :                         CHECK_LINK(id);
    1146                 :                         break;
    1147                 :                 case 2:
    1148                 :                         if (zend_get_parameters_ex(2, &db, &mysql_link)==FAILURE) {
    1149                 :                                 RETURN_FALSE;
    1150                 :                         }
    1151                 :                         id = -1;
    1152                 :                         break;
    1153                 :                 default:
    1154                 :                         WRONG_PARAM_COUNT;
    1155                 :                         break;
    1156                 :         }
    1157                 : 
    1158                 :         ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
    1159                 : 
    1160                 :         PHPMY_UNBUFFERED_QUERY_CHECK();
    1161                 : 
    1162                 :         convert_to_string_ex(db);
    1163                 : 
    1164                 :         if (mysql_create_db(&mysql->conn, Z_STRVAL_PP(db))==0) {
    1165                 :                 RETURN_TRUE;
    1166                 :         } else {
    1167                 :                 RETURN_FALSE;
    1168                 :         }
    1169                 : }
    1170                 : /* }}} */
    1171                 : 
    1172                 : /* {{{ proto bool mysql_drop_db(string database_name [, int link_identifier])
    1173                 :    Drops (delete) a MySQL database */
    1174