1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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.26 2008/12/31 11:17:40 sebastian 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 9357 : {
342 9357 : if (new_value != NULL) { /* default port */
343 0 : MySG(default_port) = atoi(new_value);
344 : } else {
345 9357 : MySG(default_port) = -1;
346 : }
347 :
348 9357 : 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 9357 : {
371 9357 : mysql_globals->num_persistent = 0;
372 9357 : mysql_globals->default_socket = NULL;
373 9357 : mysql_globals->default_host = NULL;
374 9357 : mysql_globals->default_user = NULL;
375 9357 : mysql_globals->default_password = NULL;
376 9357 : mysql_globals->connect_errno = 0;
377 9357 : mysql_globals->connect_error = NULL;
378 9357 : mysql_globals->connect_timeout = 0;
379 9357 : mysql_globals->trace_mode = 0;
380 9357 : mysql_globals->result_allocated = 0;
381 9357 : }
382 : /* }}} */
383 :
384 : /* {{{ PHP_MINIT_FUNCTION
385 : */
386 : ZEND_MODULE_STARTUP_D(mysql)
387 9357 : {
388 9357 : REGISTER_INI_ENTRIES();
389 9357 : le_result = zend_register_list_destructors_ex(_free_mysql_result, NULL, "mysql result", module_number);
390 9357 : le_link = zend_register_list_destructors_ex(_close_mysql_link, NULL, "mysql link", module_number);
391 9357 : le_plink = zend_register_list_destructors_ex(NULL, _close_mysql_plink, "mysql link persistent", module_number);
392 9357 : Z_TYPE(mysql_module_entry) = type;
393 :
394 9357 : REGISTER_LONG_CONSTANT("MYSQL_ASSOC", MYSQL_ASSOC, CONST_CS | CONST_PERSISTENT);
395 9357 : REGISTER_LONG_CONSTANT("MYSQL_NUM", MYSQL_NUM, CONST_CS | CONST_PERSISTENT);
396 9357 : REGISTER_LONG_CONSTANT("MYSQL_BOTH", MYSQL_BOTH, CONST_CS | CONST_PERSISTENT);
397 9357 : REGISTER_LONG_CONSTANT("MYSQL_CLIENT_COMPRESS", CLIENT_COMPRESS, CONST_CS | CONST_PERSISTENT);
398 : #if MYSQL_VERSION_ID >= 40000
399 9357 : REGISTER_LONG_CONSTANT("MYSQL_CLIENT_SSL", CLIENT_SSL, CONST_CS | CONST_PERSISTENT);
400 : #endif
401 9357 : REGISTER_LONG_CONSTANT("MYSQL_CLIENT_INTERACTIVE", CLIENT_INTERACTIVE, CONST_CS | CONST_PERSISTENT);
402 9357 : REGISTER_LONG_CONSTANT("MYSQL_CLIENT_IGNORE_SPACE", CLIENT_IGNORE_SPACE, CONST_CS | CONST_PERSISTENT);
403 :
404 : #if MYSQL_VERSION_ID >= 40000
405 9357 : if (mysql_server_init(0, NULL, NULL)) {
406 0 : return FAILURE;
407 : }
408 : #endif
409 :
410 9357 : return SUCCESS;
411 : }
412 : /* }}} */
413 :
414 : /* {{{ PHP_MSHUTDOWN_FUNCTION
415 : */
416 : PHP_MSHUTDOWN_FUNCTION(mysql)
417 9376 : {
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 9376 : mysql_server_end();
427 : #endif
428 : #endif
429 :
430 9376 : UNREGISTER_INI_ENTRIES();
431 9376 : return SUCCESS;
432 : }
433 : /* }}} */
434 :
435 : /* {{{ PHP_RINIT_FUNCTION
436 : */
437 : PHP_RINIT_FUNCTION(mysql)
438 9357 : {
439 : #if defined(ZTS) && MYSQL_VERSION_ID >= 40000
440 : if (mysql_thread_init()) {
441 : return FAILURE;
442 : }
443 : #endif
444 9357 : MySG(default_link)=-1;
445 9357 : MySG(num_links) = MySG(num_persistent);
446 : /* Reset connect error/errno on every request */
447 9357 : MySG(connect_error) = NULL;
448 9357 : MySG(connect_errno) =0;
449 9357 : MySG(result_allocated) = 0;
450 :
451 9357 : return SUCCESS;
452 : }
453 : /* }}} */
454 :
455 : /* {{{ PHP_RSHUTDOWN_FUNCTION
456 : */
457 : PHP_RSHUTDOWN_FUNCTION(mysql)
458 9377 : {
459 : #if defined(ZTS) && MYSQL_VERSION_ID >= 40000
460 : mysql_thread_end();
461 : #endif
462 :
463 9377 : 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 9377 : if (MySG(connect_error)!=NULL) {
470 0 : efree(MySG(connect_error));
471 : }
472 9377 : return SUCCESS;
473 : }
474 : /* }}} */
475 :
476 : /* {{{ PHP_MINFO_FUNCTION
477 : */
478 : PHP_MINFO_FUNCTION(mysql)
479 3 : {
480 : char buf[32];
481 :
482 3 : php_info_print_table_start();
483 3 : php_info_print_table_header(2, "MySQL Support", "enabled");
484 3 : snprintf(buf, sizeof(buf), "%ld", MySG(num_persistent));
485 3 : php_info_print_table_row(2, "Active Persistent Links", buf);
486 3 : snprintf(buf, sizeof(buf), "%ld", MySG(num_links));
487 3 : php_info_print_table_row(2, "Active Links", buf);
488 3 : php_info_print_table_row(2, "Client API version", mysql_get_client_info());
489 : #if !defined (PHP_WIN32) && !defined (NETWARE)
490 3 : php_info_print_table_row(2, "MYSQL_MODULE_TYPE", PHP_MYSQL_TYPE);
491 3 : php_info_print_table_row(2, "MYSQL_SOCKET", MYSQL_UNIX_ADDR);
492 3 : php_info_print_table_row(2, "MYSQL_INCLUDE", PHP_MYSQL_INCLUDE);
493 3 : php_info_print_table_row(2, "MYSQL_LIBS", PHP_MYSQL_LIBS);
494 : #endif
495 :
496 3 : php_info_print_table_end();
497 :
498 3 : DISPLAY_INI_ENTRIES();
499 :
500 3 : }
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 : PHP_FUNCTION(mysql_drop_db)
1175 : {
1176 : zval **db, **mysql_link;
1177 : int id;
1178 : php_mysql_conn *mysql;
1179 :
1180 : switch(ZEND_NUM_ARGS()) {
1181 : case 1:
1182 : if (zend_get_parameters_ex(1, &db)==FAILURE) {
1183 : RETURN_FALSE;
1184 : }
1185 : id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1186 : CHECK_LINK(id);
1187 : break;
1188 : case 2:
1189 : if (zend_get_parameters_ex(2, &db, &mysql_link)==FAILURE) {
1190 : RETURN_FALSE;
1191 : }
1192 : id = -1;
1193 : break;
1194 : default:
1195 : WRONG_PARAM_COUNT;
1196 : break;
1197 : }
1198 :
1199 : ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1200 :
1201 : convert_to_string_ex(db);
1202 :
1203 : if (mysql_drop_db(&mysql->conn, Z_STRVAL_PP(db))==0) {
1204 : RETURN_TRUE;
1205 : } else {
1206 : RETURN_FALSE;
1207 : }
1208 : }
1209 : /* }}} */
1210 : #endif
1211 : #endif /* NETWARE */
1212 :
1213 : /* {{{ php_mysql_do_query_general
1214 : */
1215 : static void php_mysql_do_query_general(zval **query, zval **mysql_link, int link_id, zval **db, int use_store, zval *return_value TSRMLS_DC)
1216 11 : {
1217 : php_mysql_conn *mysql;
1218 : MYSQL_RES *mysql_result;
1219 :
1220 11 : ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, link_id, "MySQL-Link", le_link, le_plink);
1221 :
1222 11 : if (db) {
1223 0 : convert_to_string_ex(db);
1224 0 : if (!php_mysql_select_db(mysql, Z_STRVAL_PP(db) TSRMLS_CC)) {
1225 0 : RETURN_FALSE;
1226 : }
1227 : }
1228 :
1229 11 : PHPMY_UNBUFFERED_QUERY_CHECK();
1230 :
1231 11 : convert_to_string_ex(query);
1232 :
1233 : /* check explain */
1234 11 : if (MySG(trace_mode)) {
1235 0 : if (!strncasecmp("select", Z_STRVAL_PP(query), 6)){
1236 : MYSQL_ROW row;
1237 :
1238 : char *newquery;
1239 0 : int newql = spprintf (&newquery, 0, "EXPLAIN %s", Z_STRVAL_PP(query));
1240 0 : mysql_real_query(&mysql->conn, newquery, newql);
1241 0 : efree (newquery);
1242 0 : if (mysql_errno(&mysql->conn)) {
1243 0 : php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "%s", mysql_error(&mysql->conn));
1244 0 : RETURN_FALSE;
1245 : }
1246 : else {
1247 0 : mysql_result = mysql_use_result(&mysql->conn);
1248 0 : while ((row = mysql_fetch_row(mysql_result))) {
1249 0 : if (!strcmp("ALL", row[1])) {
1250 0 : 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]);
1251 0 : } else if (!strcmp("INDEX", row[1])) {
1252 0 : 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]);
1253 : }
1254 : }
1255 0 : mysql_free_result(mysql_result);
1256 : }
1257 : }
1258 : } /* end explain */
1259 :
1260 : /* mysql_query is binary unsafe, use mysql_real_query */
1261 : #if MYSQL_VERSION_ID > 32199
1262 11 : if (mysql_real_query(&mysql->conn, Z_STRVAL_PP(query), Z_STRLEN_PP(query))!=0) {
1263 : /* check possible error */
1264 0 : if (MySG(trace_mode)){
1265 0 : if (mysql_errno(&mysql->conn)){
1266 0 : php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "%s", mysql_error(&mysql->conn));
1267 : }
1268 : }
1269 0 : RETURN_FALSE;
1270 : }
1271 : #else
1272 : if (mysql_query(&mysql->conn, Z_STRVAL_PP(query))!=0) {
1273 : /* check possible error */
1274 : if (MySG(trace_mode)){
1275 : if (mysql_errno(&mysql->conn)){
1276 : php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "%s", mysql_error(&mysql->conn));
1277 : }
1278 : }
1279 : RETURN_FALSE;
1280 : }
1281 : #endif
1282 11 : if(use_store == MYSQL_USE_RESULT) {
1283 0 : mysql_result=mysql_use_result(&mysql->conn);
1284 : } else {
1285 11 : mysql_result=mysql_store_result(&mysql->conn);
1286 : }
1287 11 : if (!mysql_result) {
1288 8 : if (PHP_MYSQL_VALID_RESULT(&mysql->conn)) { /* query should have returned rows */
1289 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save result set");
1290 0 : RETURN_FALSE;
1291 : } else {
1292 8 : RETURN_TRUE;
1293 : }
1294 : }
1295 3 : MySG(result_allocated)++;
1296 3 : ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
1297 3 : if (use_store == MYSQL_USE_RESULT) {
1298 0 : mysql->active_result_id = Z_LVAL_P(return_value);
1299 : }
1300 : }
1301 : /* }}} */
1302 :
1303 : /* {{{ php_mysql_do_query
1304 : */
1305 : static void php_mysql_do_query(INTERNAL_FUNCTION_PARAMETERS, int use_store)
1306 11 : {
1307 : zval **query, **mysql_link;
1308 : int id;
1309 :
1310 11 : switch(ZEND_NUM_ARGS()) {
1311 : case 1:
1312 11 : if (zend_get_parameters_ex(1, &query)==FAILURE) {
1313 0 : RETURN_FALSE;
1314 : }
1315 11 : id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1316 11 : CHECK_LINK(id);
1317 11 : break;
1318 : case 2:
1319 0 : if (zend_get_parameters_ex(2, &query, &mysql_link)==FAILURE) {
1320 0 : RETURN_FALSE;
1321 : }
1322 0 : id = -1;
1323 0 : break;
1324 : default:
1325 0 : WRONG_PARAM_COUNT;
1326 : break;
1327 : }
1328 11 : php_mysql_do_query_general(query, mysql_link, id, NULL, use_store, return_value TSRMLS_CC);
1329 : }
1330 : /* }}} */
1331 :
1332 : /* {{{ proto resource mysql_query(string query [, int link_identifier])
1333 : Sends an SQL query to MySQL */
1334 : PHP_FUNCTION(mysql_query)
1335 11 : {
1336 11 : php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_STORE_RESULT);
1337 11 : }
1338 : /* }}} */
1339 :
1340 :
1341 : /* {{{ proto resource mysql_unbuffered_query(string query [, int link_identifier])
1342 : Sends an SQL query to MySQL, without fetching and buffering the result rows */
1343 : PHP_FUNCTION(mysql_unbuffered_query)
1344 0 : {
1345 0 : php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_USE_RESULT);
1346 0 : }
1347 : /* }}} */
1348 :
1349 :
1350 : /* {{{ proto resource mysql_db_query(string database_name, string query [, int link_identifier])
1351 : Sends an SQL query to MySQL */
1352 : PHP_FUNCTION(mysql_db_query)
1353 0 : {
1354 : zval **db, **query, **mysql_link;
1355 : int id;
1356 :
1357 0 : switch(ZEND_NUM_ARGS()) {
1358 : case 2:
1359 0 : if (zend_get_parameters_ex(2, &db, &query)==FAILURE) {
1360 0 : RETURN_FALSE;
1361 : }
1362 0 : id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1363 0 : CHECK_LINK(id);
1364 0 : break;
1365 : case 3:
1366 0 : if (zend_get_parameters_ex(3, &db, &query, &mysql_link)==FAILURE) {
1367 0 : RETURN_FALSE;
1368 : }
1369 0 : id = -1;
1370 0 : break;
1371 : default:
1372 0 : WRONG_PARAM_COUNT;
1373 : break;
1374 : }
1375 :
1376 0 : if (MySG(trace_mode) || !strcasecmp(get_active_function_name(TSRMLS_C), "mysql")) {
1377 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "This function is deprecated; use mysql_query() instead.");
1378 : }
1379 :
1380 0 : php_mysql_do_query_general(query, mysql_link, id, db, MYSQL_STORE_RESULT, return_value TSRMLS_CC);
1381 : }
1382 : /* }}} */
1383 :
1384 :
1385 : /* {{{ proto resource mysql_list_dbs([int link_identifier])
1386 : List databases available on a MySQL server */
1387 : PHP_FUNCTION(mysql_list_dbs)
1388 0 : {
1389 : zval **mysql_link;
1390 : int id;
1391 : php_mysql_conn *mysql;
1392 : MYSQL_RES *mysql_result;
1393 :
1394 0 : switch(ZEND_NUM_ARGS()) {
1395 : case 0:
1396 0 : id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1397 0 : CHECK_LINK(id);
1398 0 : break;
1399 : case 1:
1400 0 : if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
1401 0 : RETURN_FALSE;
1402 : }
1403 0 : id = -1;
1404 0 : break;
1405 : default:
1406 0 : WRONG_PARAM_COUNT;
1407 : break;
1408 : }
1409 :
1410 0 : ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1411 :
1412 0 : PHPMY_UNBUFFERED_QUERY_CHECK();
1413 :
1414 0 : if ((mysql_result=mysql_list_dbs(&mysql->conn, NULL))==NULL) {
1415 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save MySQL query result");
1416 0 : RETURN_FALSE;
1417 : }
1418 0 : ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
1419 : }
1420 : /* }}} */
1421 :
1422 :
1423 : /* {{{ proto resource mysql_list_tables(string database_name [, int link_identifier])
1424 : List tables in a MySQL database */
1425 : PHP_FUNCTION(mysql_list_tables)
1426 0 : {
1427 : zval **db, **mysql_link;
1428 : int id;
1429 : php_mysql_conn *mysql;
1430 : MYSQL_RES *mysql_result;
1431 :
1432 0 : switch(ZEND_NUM_ARGS()) {
1433 : case 1:
1434 0 : if (zend_get_parameters_ex(1, &db)==FAILURE) {
1435 0 : RETURN_FALSE;
1436 : }
1437 0 : id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1438 0 : CHECK_LINK(id);
1439 0 : break;
1440 : case 2:
1441 0 : if (zend_get_parameters_ex(2, &db, &mysql_link)==FAILURE) {
1442 0 : RETURN_FALSE;
1443 : }
1444 0 : id = -1;
1445 0 : break;
1446 : default:
1447 0 : WRONG_PARAM_COUNT;
1448 : break;
1449 : }
1450 0 : ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1451 :
1452 0 : convert_to_string_ex(db);
1453 0 : if (!php_mysql_select_db(mysql, Z_STRVAL_PP(db) TSRMLS_CC)) {
1454 0 : RETURN_FALSE;
1455 : }
1456 :
1457 0 : PHPMY_UNBUFFERED_QUERY_CHECK();
1458 :
1459 0 : if ((mysql_result=mysql_list_tables(&mysql->conn, NULL))==NULL) {
1460 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save MySQL query result");
1461 0 : RETURN_FALSE;
1462 : }
1463 0 : ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
1464 : }
1465 : /* }}} */
1466 :
1467 :
1468 : /* {{{ proto resource mysql_list_fields(string database_name, string table_name [, int link_identifier])
1469 : List MySQL result fields */
1470 : PHP_FUNCTION(mysql_list_fields)
1471 0 : {
1472 : zval **db, **table, **mysql_link;
1473 : int id;
1474 : php_mysql_conn *mysql;
1475 : MYSQL_RES *mysql_result;
1476 :
1477 0 : switch(ZEND_NUM_ARGS()) {
1478 : case 2:
1479 0 : if (zend_get_parameters_ex(2, &db, &table)==FAILURE) {
1480 0 : RETURN_FALSE;
1481 : }
1482 0 : id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1483 0 : CHECK_LINK(id);
1484 0 : break;
1485 : case 3:
1486 0 : if (zend_get_parameters_ex(3, &db, &table, &mysql_link)==FAILURE) {
1487 0 : RETURN_FALSE;
1488 : }
1489 0 : id = -1;
1490 0 : break;
1491 : default:
1492 0 : WRONG_PARAM_COUNT;
1493 : break;
1494 : }
1495 :
1496 0 : ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1497 :
1498 0 : convert_to_string_ex(db);
1499 0 : if (!php_mysql_select_db(mysql, Z_STRVAL_PP(db) TSRMLS_CC)) {
1500 0 : RETURN_FALSE;
1501 : }
1502 :
1503 0 : PHPMY_UNBUFFERED_QUERY_CHECK();
1504 :
1505 0 : convert_to_string_ex(table);
1506 0 : if ((mysql_result=mysql_list_fields(&mysql->conn, Z_STRVAL_PP(table), NULL))==NULL) {
1507 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save MySQL query result");
1508 0 : RETURN_FALSE;
1509 : }
1510 0 : ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
1511 : }
1512 : /* }}} */
1513 :
1514 : /* {{{ proto resource mysql_list_processes([int link_identifier])
1515 : Returns a result set describing the current server threads */
1516 : PHP_FUNCTION(mysql_list_processes)
1517 0 : {
1518 0 : zval *mysql_link = NULL;
1519 0 : int id = -1;
1520 : php_mysql_conn *mysql;
1521 : MYSQL_RES *mysql_result;
1522 :
1523 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
1524 0 : return;
1525 : }
1526 :
1527 0 : if (ZEND_NUM_ARGS() == 0) {
1528 0 : id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1529 0 : CHECK_LINK(id);
1530 : }
1531 :
1532 0 : ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
1533 :
1534 0 : PHPMY_UNBUFFERED_QUERY_CHECK();
1535 :
1536 0 : mysql_result = mysql_list_processes(&mysql->conn);
1537 0 : if (mysql_result == NULL) {
1538 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save MySQL query result");
1539 0 : RETURN_FALSE;
1540 : }
1541 :
1542 0 : ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
1543 : }
1544 : /* }}} */
1545 :
1546 :
1547 : /* {{{ proto string mysql_error([int link_identifier])
1548 : Returns the text of the error message from previous MySQL operation */
1549 : PHP_FUNCTION(mysql_error)
1550 0 : {
1551 : zval **mysql_link;
1552 : int id;
1553 : php_mysql_conn *mysql;
1554 :
1555 0 : switch(ZEND_NUM_ARGS()) {
1556 : case 0:
1557 0 : id = MySG(default_link);
1558 0 : if (id==-1) {
1559 0 : if (MySG(connect_error)!=NULL){
1560 0 : RETURN_STRING(MySG(connect_error),1);
1561 : } else {
1562 0 : RETURN_FALSE;
1563 : }
1564 : }
1565 0 : break;
1566 : case 1:
1567 0 : if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
1568 0 : RETURN_FALSE;
1569 : }
1570 0 : id = -1;
1571 0 : break;
1572 : default:
1573 0 : WRONG_PARAM_COUNT;
1574 : break;
1575 : }
1576 :
1577 0 : ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1578 :
1579 0 : RETURN_STRING((char *)mysql_error(&mysql->conn), 1);
1580 : }
1581 : /* }}} */
1582 :
1583 :
1584 : /* {{{ proto int mysql_errno([int link_identifier])
1585 : Returns the number of the error message from previous MySQL operation */
1586 : #ifdef HAVE_MYSQL_ERRNO
1587 : PHP_FUNCTION(mysql_errno)
1588 0 : {
1589 : zval **mysql_link;
1590 : int id;
1591 : php_mysql_conn *mysql;
1592 :
1593 0 : switch(ZEND_NUM_ARGS()) {
1594 : case 0:
1595 0 : id = MySG(default_link);
1596 0 : if (id==-1) {
1597 0 : if (MySG(connect_errno)!=0){
1598 0 : RETURN_LONG(MySG(connect_errno));
1599 : } else {
1600 0 : RETURN_FALSE;
1601 : }
1602 : }
1603 0 : break;
1604 : case 1:
1605 0 : if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
1606 0 : RETURN_FALSE;
1607 : }
1608 0 : id = -1;
1609 0 : break;
1610 : default:
1611 0 : WRONG_PARAM_COUNT;
1612 : break;
1613 : }
1614 :
1615 0 : ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1616 :
1617 0 : RETURN_LONG(mysql_errno(&mysql->conn));
1618 : }
1619 : #endif
1620 : /* }}} */
1621 :
1622 :
1623 : /* {{{ proto int mysql_affected_rows([int link_identifier])
1624 : Gets number of affected rows in previous MySQL operation */
1625 : PHP_FUNCTION(mysql_affected_rows)
1626 0 : {
1627 : zval **mysql_link;
1628 : int id;
1629 : php_mysql_conn *mysql;
1630 :
1631 0 : switch(ZEND_NUM_ARGS()) {
1632 : case 0:
1633 0 : id = MySG(default_link);
1634 0 : CHECK_LINK(id);
1635 0 : break;
1636 : case 1:
1637 0 : if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
1638 0 : RETURN_FALSE;
1639 : }
1640 0 : id = -1;
1641 0 : break;
1642 : default:
1643 0 : WRONG_PARAM_COUNT;
1644 : break;
1645 : }
1646 :
1647 0 : ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1648 :
1649 : /* conversion from int64 to long happing here */
1650 0 : Z_LVAL_P(return_value) = (long) mysql_affected_rows(&mysql->conn);
1651 0 : Z_TYPE_P(return_value) = IS_LONG;
1652 : }
1653 : /* }}} */
1654 :
1655 :
1656 : /* {{{ proto string mysql_escape_string(string to_be_escaped)
1657 : Escape string for mysql query */
1658 : PHP_FUNCTION(mysql_escape_string)
1659 0 : {
1660 : zval **str;
1661 :
1662 0 : if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &str) == FAILURE) {
1663 0 : ZEND_WRONG_PARAM_COUNT();
1664 : }
1665 0 : convert_to_string_ex(str);
1666 : /* assume worst case situation, which is 2x of the original string.
1667 : * we don't realloc() down to the real size since it'd most probably not
1668 : * be worth it
1669 : */
1670 :
1671 0 : Z_STRVAL_P(return_value) = (char *) safe_emalloc(Z_STRLEN_PP(str), 2, 1);
1672 0 : Z_STRLEN_P(return_value) = mysql_escape_string(Z_STRVAL_P(return_value), Z_STRVAL_PP(str), Z_STRLEN_PP(str));
1673 0 : Z_TYPE_P(return_value) = IS_STRING;
1674 :
1675 0 : if (MySG(trace_mode)){
1676 0 : php_error_docref("function.mysql-real-escape-string" TSRMLS_CC, E_WARNING, "This function is deprecated; use mysql_real_escape_string() instead.");
1677 : }
1678 :
1679 : }
1680 : /* }}} */
1681 :
1682 : /* {{{ proto string mysql_real_escape_string(string to_be_escaped [, int link_identifier])
1683 : Escape special characters in a string for use in a SQL statement, taking into account the current charset of the connection */
1684 : PHP_FUNCTION(mysql_real_escape_string)
1685 0 : {
1686 0 : zval *mysql_link = NULL;
1687 : char *str;
1688 : char *new_str;
1689 0 : int id = -1, str_len, new_str_len;
1690 : php_mysql_conn *mysql;
1691 :
1692 :
1693 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &str, &str_len, &mysql_link) == FAILURE) {
1694 0 : return;
1695 : }
1696 :
1697 0 : if (ZEND_NUM_ARGS() == 1) {
1698 0 : id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1699 0 : CHECK_LINK(id);
1700 : }
1701 :
1702 0 : ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
1703 :
1704 0 : new_str = safe_emalloc(str_len, 2, 1);
1705 0 : new_str_len = mysql_real_escape_string(&mysql->conn, new_str, str, str_len);
1706 0 : new_str = erealloc(new_str, new_str_len + 1);
1707 :
1708 0 : RETURN_STRINGL(new_str, new_str_len, 0);
1709 : }
1710 : /* }}} */
1711 :
1712 : /* {{{ proto int mysql_insert_id([int link_identifier])
1713 : Gets the ID generated from the previous INSERT operation */
1714 : PHP_FUNCTION(mysql_insert_id)
1715 0 : {
1716 : zval **mysql_link;
1717 : int id;
1718 : php_mysql_conn *mysql;
1719 :
1720 0 : switch(ZEND_NUM_ARGS()) {
1721 : case 0:
1722 0 : id = MySG(default_link);
1723 0 : CHECK_LINK(id);
1724 0 : break;
1725 : case 1:
1726 0 : if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
1727 0 : RETURN_FALSE;
1728 : }
1729 0 : id = -1;
1730 0 : break;
1731 : default:
1732 0 : WRONG_PARAM_COUNT;
1733 : break;
1734 : }
1735 :
1736 0 : ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1737 :
1738 : /* conversion from int64 to long happing here */
1739 0 : Z_LVAL_P(return_value) = (long) mysql_insert_id(&mysql->conn);
1740 0 : Z_TYPE_P(return_value) = IS_LONG;
1741 : }
1742 : /* }}} */
1743 :
1744 :
1745 : /* {{{ proto mixed mysql_result(resource result, int row [, mixed field])
1746 : Gets result data */
1747 : PHP_FUNCTION(mysql_result)
1748 0 : {
1749 0 : zval **result, **row, **field=NULL;
1750 : MYSQL_RES *mysql_result;
1751 : MYSQL_ROW sql_row;
1752 : mysql_row_length_type *sql_row_lengths;
1753 0 : int field_offset=0;
1754 :
1755 0 : switch (ZEND_NUM_ARGS()) {
1756 : case 2:
1757 0 : if (zend_get_parameters_ex(2, &result, &row)==FAILURE) {
1758 0 : RETURN_FALSE;
1759 : }
1760 0 : break;
1761 : case 3:
1762 0 : if (zend_get_parameters_ex(3, &result, &row, &field)==FAILURE) {
1763 0 : RETURN_FALSE;
1764 : }
1765 0 : break;
1766 : default:
1767 0 : WRONG_PARAM_COUNT;
1768 : break;
1769 : }
1770 :
1771 0 : ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
1772 :
1773 0 : convert_to_long_ex(row);
1774 0 : if (Z_LVAL_PP(row)<0 || Z_LVAL_PP(row)>=(int)mysql_num_rows(mysql_result)) {
1775 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on MySQL result index %ld", Z_LVAL_PP(row), Z_LVAL_PP(result));
1776 0 : RETURN_FALSE;
1777 : }
1778 0 : mysql_data_seek(mysql_result, Z_LVAL_PP(row));
1779 0 : if ((sql_row=mysql_fetch_row(mysql_result))==NULL
1780 : || (sql_row_lengths=mysql_fetch_lengths(mysql_result))==NULL) { /* shouldn't happen? */
1781 0 : RETURN_FALSE;
1782 : }
1783 :
1784 0 : if (field) {
1785 0 : switch(Z_TYPE_PP(field)) {
1786 : case IS_STRING: {
1787 0 : int i=0;
1788 : MYSQL_FIELD *tmp_field;
1789 : char *table_name, *field_name, *tmp;
1790 :
1791 0 : if ((tmp=strchr(Z_STRVAL_PP(field), '.'))) {
1792 0 : table_name = estrndup(Z_STRVAL_PP(field), tmp-Z_STRVAL_PP(field));
1793 0 : field_name = estrdup(tmp+1);
1794 : } else {
1795 0 : table_name = NULL;
1796 0 : field_name = estrndup(Z_STRVAL_PP(field),Z_STRLEN_PP(field));
1797 : }
1798 0 : mysql_field_seek(mysql_result, 0);
1799 0 : while ((tmp_field=mysql_fetch_field(mysql_result))) {
1800 0 : if ((!table_name || !strcasecmp(tmp_field->table, table_name)) && !strcasecmp(tmp_field->name, field_name)) {
1801 0 : field_offset = i;
1802 0 : break;
1803 : }
1804 0 : i++;
1805 : }
1806 0 : if (!tmp_field) { /* no match found */
1807 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s%s%s not found in MySQL result index %ld",
1808 : (table_name?table_name:""), (table_name?".":""), field_name, Z_LVAL_PP(result));
1809 0 : efree(field_name);
1810 0 : if (table_name) {
1811 0 : efree(table_name);
1812 : }
1813 0 : RETURN_FALSE;
1814 : }
1815 0 : efree(field_name);
1816 0 : if (table_name) {
1817 0 : efree(table_name);
1818 : }
1819 : }
1820 0 : break;
1821 : default:
1822 0 : convert_to_long_ex(field);
1823 0 : field_offset = Z_LVAL_PP(field);
1824 0 : if (field_offset<0 || field_offset>=(int)mysql_num_fields(mysql_result)) {
1825 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified");
1826 0 : RETURN_FALSE;
1827 : }
1828 : break;
1829 : }
1830 : }
1831 :
1832 0 : if (sql_row[field_offset]) {
1833 0 : Z_TYPE_P(return_value) = IS_STRING;
1834 :
1835 0 : if (PG(magic_quotes_runtime)) {
1836 0 : Z_STRVAL_P(return_value) = php_addslashes(sql_row[field_offset], sql_row_lengths[field_offset],&Z_STRLEN_P(return_value), 0 TSRMLS_CC);
1837 : } else {
1838 0 : Z_STRLEN_P(return_value) = sql_row_lengths[field_offset];
1839 0 : Z_STRVAL_P(return_value) = (char *) safe_estrndup(sql_row[field_offset], Z_STRLEN_P(return_value));
1840 : }
1841 : } else {
1842 0 : Z_TYPE_P(return_value) = IS_NULL;
1843 : }
1844 : }
1845 : /* }}} */
1846 :
1847 :
1848 : /* {{{ proto int mysql_num_rows(resource result)
1849 : Gets number of rows in a result */
1850 : PHP_FUNCTION(mysql_num_rows)
1851 0 : {
1852 : zval **result;
1853 : MYSQL_RES *mysql_result;
1854 :
1855 0 : if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &result)==FAILURE) {
1856 0 : WRONG_PARAM_COUNT;
1857 : }
1858 :
1859 0 : ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
1860 :
1861 : /* conversion from int64 to long happing here */
1862 0 : Z_LVAL_P(return_value) = (long) mysql_num_rows(mysql_result);
1863 0 : Z_TYPE_P(return_value) = IS_LONG;
1864 : }
1865 : /* }}} */
1866 :
1867 : /* {{{ proto int mysql_num_fields(resource result)
1868 : Gets number of fields in a result */
1869 : PHP_FUNCTION(mysql_num_fields)
1870 0 : {
1871 : zval **result;
1872 : MYSQL_RES *mysql_result;
1873 :
1874 0 : if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &result)==FAILURE) {
1875 0 : WRONG_PARAM_COUNT;
1876 : }
1877 :
1878 0 : ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
1879 :
1880 0 : Z_LVAL_P(return_value) = mysql_num_fields(mysql_result);
1881 0 : Z_TYPE_P(return_value) = IS_LONG;
1882 : }
1883 : /* }}} */
1884 :
1885 : /* {{{ php_mysql_fetch_hash
1886 : */
1887 : static void php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type, int expected_args, int into_object)
1888 11 : {
1889 : zval **result, **arg2;
1890 : MYSQL_RES *mysql_result;
1891 : MYSQL_ROW mysql_row;
1892 : MYSQL_FIELD *mysql_field;
1893 : mysql_row_length_type *mysql_row_lengths;
1894 : int i;
1895 11 : zval *res, *ctor_params = NULL;
1896 11 : zend_class_entry *ce = NULL;
1897 :
1898 : #ifdef ZEND_ENGINE_2
1899 11 : if (into_object) {
1900 : char *class_name;
1901 : int class_name_len;
1902 :
1903 8 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|sz", &res, &class_name, &class_name_len, &ctor_params) == FAILURE) {
1904 0 : return;
1905 : }
1906 8 : result = &res;
1907 8 : if (ZEND_NUM_ARGS() < 2) {
1908 4 : ce = zend_standard_class_def;
1909 : } else {
1910 4 : ce = zend_fetch_class(class_name, class_name_len, ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
1911 : }
1912 8 : if (!ce) {
1913 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not find class '%s'", class_name);
1914 0 : return;
1915 : }
1916 8 : result_type = MYSQL_ASSOC;
1917 : } else
1918 : #endif
1919 : {
1920 3 : if (ZEND_NUM_ARGS() > expected_args) {
1921 0 : WRONG_PARAM_COUNT;
1922 : }
1923 :
1924 3 : switch (ZEND_NUM_ARGS()) {
1925 : case 1:
1926 0 : if (zend_get_parameters_ex(1, &result)==FAILURE) {
1927 0 : RETURN_FALSE;
1928 : }
1929 0 : if (!result_type) {
1930 0 : result_type = MYSQL_BOTH;
1931 : }
1932 0 : break;
1933 : case 2:
1934 3 : if (zend_get_parameters_ex(2, &result, &arg2)==FAILURE) {
1935 0 : RETURN_FALSE;
1936 : }
1937 3 : convert_to_long_ex(arg2);
1938 3 : result_type = Z_LVAL_PP(arg2);
1939 3 : break;
1940 : default:
1941 0 : WRONG_PARAM_COUNT;
1942 : break;
1943 : }
1944 : }
1945 :
1946 11 : if ((result_type & MYSQL_BOTH) == 0) {
1947 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "The result type should be either MYSQL_NUM, MYSQL_ASSOC or MYSQL_BOTH.");
1948 : }
1949 :
1950 11 : ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
1951 :
1952 11 : if ((mysql_row=mysql_fetch_row(mysql_result))==NULL
1953 : || (mysql_row_lengths=mysql_fetch_lengths(mysql_result))==NULL) {
1954 3 : RETURN_FALSE;
1955 : }
1956 :
1957 8 : array_init(return_value);
1958 :
1959 8 : mysql_field_seek(mysql_result, 0);
1960 20 : for (mysql_field=mysql_fetch_field(mysql_result), i=0; mysql_field; mysql_field=mysql_fetch_field(mysql_result), i++) {
1961 12 : if (mysql_row[i]) {
1962 : zval *data;
1963 :
1964 12 : MAKE_STD_ZVAL(data);
1965 :
1966 12 : if (PG(magic_quotes_runtime)) {
1967 0 : Z_TYPE_P(data) = IS_STRING;
1968 0 : Z_STRVAL_P(data) = php_addslashes(mysql_row[i], mysql_row_lengths[i], &Z_STRLEN_P(data), 0 TSRMLS_CC);
1969 : } else {
1970 12 : ZVAL_STRINGL(data, mysql_row[i], mysql_row_lengths[i], 1);
1971 : }
1972 :
1973 12 : if (result_type & MYSQL_NUM) {
1974 0 : add_index_zval(return_value, i, data);
1975 : }
1976 12 : if (result_type & MYSQL_ASSOC) {
1977 12 : if (result_type & MYSQL_NUM) {
1978 0 : ZVAL_ADDREF(data);
1979 : }
1980 12 : add_assoc_zval(return_value, mysql_field->name, data);
1981 : }
1982 : } else {
1983 : /* NULL value. */
1984 0 : if (result_type & MYSQL_NUM) {
1985 0 : add_index_null(return_value, i);
1986 : }
1987 :
1988 0 : if (result_type & MYSQL_ASSOC) {
1989 0 : add_assoc_null(return_value, mysql_field->name);
1990 : }
1991 : }
1992 : }
1993 :
1994 : #ifdef ZEND_ENGINE_2
1995 8 : if (into_object) {
1996 6 : zval dataset = *return_value;
1997 : zend_fcall_info fci;
1998 : zend_fcall_info_cache fcc;
1999 : zval *retval_ptr;
2000 :
2001 6 : object_and_properties_init(return_value, ce, NULL);
2002 6 : zend_merge_properties(return_value, Z_ARRVAL(dataset), 1 TSRMLS_CC);
2003 :
2004 6 : if (ce->constructor) {
2005 3 : fci.size = sizeof(fci);
2006 3 : fci.function_table = &ce->function_table;
2007 3 : fci.function_name = NULL;
2008 3 : fci.symbol_table = NULL;
2009 3 : fci.object_pp = &return_value;
2010 3 : fci.retval_ptr_ptr = &retval_ptr;
2011 3 : if (ctor_params && Z_TYPE_P(ctor_params) != IS_NULL) {
2012 0 : if (Z_TYPE_P(ctor_params) == IS_ARRAY) {
2013 0 : HashTable *ht = Z_ARRVAL_P(ctor_params);
2014 : Bucket *p;
2015 :
2016 0 : fci.param_count = 0;
2017 0 : fci.params = safe_emalloc(sizeof(zval*), ht->nNumOfElements, 0);
2018 0 : p = ht->pListHead;
2019 0 : while (p != NULL) {
2020 0 : fci.params[fci.param_count++] = (zval**)p->pData;
2021 0 : p = p->pListNext;
2022 : }
2023 : } else {
2024 : /* Two problems why we throw exceptions here: PHP is typeless
2025 : * and hence passing one argument that's not an array could be
2026 : * by mistake and the other way round is possible, too. The
2027 : * single value is an array. Also we'd have to make that one
2028 : * argument passed by reference.
2029 : */
2030 0 : zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Parameter ctor_params must be an array", 0 TSRMLS_CC);
2031 0 : return;
2032 : }
2033 : } else {
2034 3 : fci.param_count = 0;
2035 3 : fci.params = NULL;
2036 : }
2037 3 : fci.no_separation = 1;
2038 :
2039 3 : fcc.initialized = 1;
2040 3 : fcc.function_handler = ce->constructor;
2041 3 : fcc.calling_scope = EG(scope);
2042 3 : fcc.object_pp = &return_value;
2043 :
2044 3 : if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
2045 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);
2046 : } else {
2047 3 : if (retval_ptr) {
2048 3 : zval_ptr_dtor(&retval_ptr);
2049 : }
2050 : }
2051 3 : if (fci.params) {
2052 0 : efree(fci.params);
2053 : }
2054 3 : } else if (ctor_params) {
2055 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);
2056 : }
2057 : }
2058 : #endif
2059 :
2060 : }
2061 : /* }}} */
2062 :
2063 : /* {{{ proto array mysql_fetch_row(resource result)
2064 : Gets a result row as an enumerated array */
2065 : PHP_FUNCTION(mysql_fetch_row)
2066 0 : {
2067 0 : php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_NUM, 1, 0);
2068 0 : }
2069 : /* }}} */
2070 :
2071 :
2072 : /* {{{ proto object mysql_fetch_object(resource result [, string class_name [, NULL|array ctor_params]])
2073 : Fetch a result row as an object */
2074 : PHP_FUNCTION(mysql_fetch_object)
2075 8 : {
2076 8 : php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_ASSOC, 2, 1);
2077 :
2078 8 : if (Z_TYPE_P(return_value) == IS_ARRAY) {
2079 0 : object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));
2080 : }
2081 8 : }
2082 : /* }}} */
2083 :
2084 :
2085 : /* {{{ proto array mysql_fetch_array(resource result [, int result_type])
2086 : Fetch a result row as an array (associative, numeric or both) */
2087 : PHP_FUNCTION(mysql_fetch_array)
2088 3 : {
2089 3 : php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 2, 0);
2090 3 : }
2091 : /* }}} */
2092 :
2093 :
2094 : /* {{{ proto array mysql_fetch_assoc(resource result)
2095 : Fetch a result row as an associative array */
2096 : PHP_FUNCTION(mysql_fetch_assoc)
2097 0 : {
2098 0 : php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_ASSOC, 1, 0);
2099 0 : }
2100 : /* }}} */
2101 :
2102 : /* {{{ proto bool mysql_data_seek(resource result, int row_number)
2103 : Move internal result pointer */
2104 : PHP_FUNCTION(mysql_data_seek)
2105 0 : {
2106 : zval **result, **offset;
2107 : MYSQL_RES *mysql_result;
2108 :
2109 0 : if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &result, &offset)==FAILURE) {
2110 0 : WRONG_PARAM_COUNT;
2111 : }
2112 :
2113 0 : ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
2114 :
2115 0 : convert_to_long_ex(offset);
2116 0 : if (Z_LVAL_PP(offset)<0 || Z_LVAL_PP(offset)>=(int)mysql_num_rows(mysql_result)) {
2117 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset %ld is invalid for MySQL result index %ld (or the query data is unbuffered)", Z_LVAL_PP(offset), Z_LVAL_PP(result));
2118 0 : RETURN_FALSE;
2119 : }
2120 0 : mysql_data_seek(mysql_result, Z_LVAL_PP(offset));
2121 0 : RETURN_TRUE;
2122 : }
2123 : /* }}} */
2124 :
2125 :
2126 : /* {{{ proto array mysql_fetch_lengths(resource result)
2127 : Gets max data size of each column in a result */
2128 : PHP_FUNCTION(mysql_fetch_lengths)
2129 0 : {
2130 : zval **result;
2131 : MYSQL_RES *mysql_result;
2132 : mysql_row_length_type *lengths;
2133 : int num_fields;
2134 : int i;
2135 :
2136 0 : if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &result)==FAILURE) {
2137 0 : WRONG_PARAM_COUNT;
2138 : }
2139 :
2140 0 : ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
2141 :
2142 0 : if ((lengths=mysql_fetch_lengths(mysql_result))==NULL) {
2143 0 : RETURN_FALSE;
2144 : }
2145 0 : array_init(return_value);
2146 0 : num_fields = mysql_num_fields(mysql_result);
2147 :
2148 0 : for (i=0; i<num_fields; i++) {
2149 0 : add_index_long(return_value, i, lengths[i]);
2150 : }
2151 : }
2152 : /* }}} */
2153 :
2154 : /* {{{ php_mysql_get_field_name
2155 : */
2156 : static char *php_mysql_get_field_name(int field_type)
2157 0 : {
2158 0 : switch(field_type) {
2159 : case FIELD_TYPE_STRING:
2160 : case FIELD_TYPE_VAR_STRING:
2161 0 : return "string";
2162 : break;
2163 : #ifdef MYSQL_HAS_TINY
2164 : case FIELD_TYPE_TINY:
2165 : #endif
2166 : case FIELD_TYPE_SHORT:
2167 : case FIELD_TYPE_LONG:
2168 : case FIELD_TYPE_LONGLONG:
2169 : case FIELD_TYPE_INT24:
2170 0 : return "int";
2171 : break;
2172 : case FIELD_TYPE_FLOAT:
2173 : case FIELD_TYPE_DOUBLE:
2174 : case FIELD_TYPE_DECIMAL:
2175 : #ifdef FIELD_TYPE_NEWDECIMAL
2176 : case FIELD_TYPE_NEWDECIMAL:
2177 : #endif
2178 0 : return "real";
2179 : break;
2180 : case FIELD_TYPE_TIMESTAMP:
2181 0 : return "timestamp";
2182 : break;
2183 : #ifdef MYSQL_HAS_YEAR
2184 : case FIELD_TYPE_YEAR:
2185 0 : return "year";
2186 : break;
2187 : #endif
2188 : case FIELD_TYPE_DATE:
2189 : #ifdef FIELD_TYPE_NEWDATE
2190 : case FIELD_TYPE_NEWDATE:
2191 : #endif
2192 0 : return "date";
2193 : break;
2194 : case FIELD_TYPE_TIME:
2195 0 : return "time";
2196 : break;
2197 : case FIELD_TYPE_SET:
2198 0 : return "set";
2199 : break;
2200 : case FIELD_TYPE_ENUM:
2201 0 : return "enum";
2202 : break;
2203 : #ifdef FIELD_TYPE_GEOMETRY
2204 : case FIELD_TYPE_GEOMETRY:
2205 0 : return "geometry";
2206 : break;
2207 : #endif
2208 : case FIELD_TYPE_DATETIME:
2209 0 : return "datetime";
2210 : break;
2211 : case FIELD_TYPE_TINY_BLOB:
2212 : case FIELD_TYPE_MEDIUM_BLOB:
2213 : case FIELD_TYPE_LONG_BLOB:
2214 : case FIELD_TYPE_BLOB:
2215 0 : return "blob";
2216 : break;
2217 : case FIELD_TYPE_NULL:
2218 0 : return "null";
2219 : break;
2220 : default:
2221 0 : return "unknown";
2222 : break;
2223 : }
2224 : }
2225 : /* }}} */
2226 :
2227 : /* {{{ proto object mysql_fetch_field(resource result [, int field_offset])
2228 : Gets column information from a result and return as an object */
2229 : PHP_FUNCTION(mysql_fetch_field)
2230 0 : {
2231 0 : zval **result, **field=NULL;
2232 : MYSQL_RES *mysql_result;
2233 : MYSQL_FIELD *mysql_field;
2234 :
2235 0 : switch (ZEND_NUM_ARGS()) {
2236 : case 1:
2237 0 : if (zend_get_parameters_ex(1, &result)==FAILURE) {
2238 0 : RETURN_FALSE;
2239 : }
2240 0 : break;
2241 : case 2:
2242 0 : if (zend_get_parameters_ex(2, &result, &field)==FAILURE) {
2243 0 : RETURN_FALSE;
2244 : }
2245 0 : convert_to_long_ex(field);
2246 0 : break;
2247 : default:
2248 0 : WRONG_PARAM_COUNT;
2249 : }
2250 :
2251 0 : ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
2252 :
2253 0 : if (field) {
2254 0 : if (Z_LVAL_PP(field)<0 || Z_LVAL_PP(field)>=(int)mysql_num_fields(mysql_result)) {
2255 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad field offset");
2256 0 : RETURN_FALSE;
2257 : }
2258 0 : mysql_field_seek(mysql_result, Z_LVAL_PP(field));
2259 : }
2260 0 : if ((mysql_field=mysql_fetch_field(mysql_result))==NULL) {
2261 0 : RETURN_FALSE;
2262 : }
2263 0 : object_init(return_value);
2264 :
2265 0 : add_property_string(return_value, "name",(mysql_field->name?mysql_field->name:""), 1);
2266 0 : add_property_string(return_value, "table",(mysql_field->table?mysql_field->table:""), 1);
2267 0 : add_property_string(return_value, "def",(mysql_field->def?mysql_field->def:""), 1);
2268 0 : add_property_long(return_value, "max_length", mysql_field->max_length);
2269 0 : add_property_long(return_value, "not_null", IS_NOT_NULL(mysql_field->flags)?1:0);
2270 0 : add_property_long(return_value, "primary_key", IS_PRI_KEY(mysql_field->flags)?1:0);
2271 0 : add_property_long(return_value, "multiple_key",(mysql_field->flags&MULTIPLE_KEY_FLAG?1:0));
2272 0 : add_property_long(return_value, "unique_key",(mysql_field->flags&UNIQUE_KEY_FLAG?1:0));
2273 0 : add_property_long(return_value, "numeric", IS_NUM(Z_TYPE_P(mysql_field))?1:0);
2274 0 : add_property_long(return_value, "blob", IS_BLOB(mysql_field->flags)?1:0);
2275 0 : add_property_string(return_value, "type", php_mysql_get_field_name(Z_TYPE_P(mysql_field)), 1);
2276 0 : add_property_long(return_value, "unsigned",(mysql_field->flags&UNSIGNED_FLAG?1:0));
2277 0 : add_property_long(return_value, "zerofill",(mysql_field->flags&ZEROFILL_FLAG?1:0));
2278 : }
2279 : /* }}} */
2280 :
2281 :
2282 : /* {{{ proto bool mysql_field_seek(resource result, int field_offset)
2283 : Sets result pointer to a specific field offset */
2284 : PHP_FUNCTION(mysql_field_seek)
2285 0 : {
2286 : zval **result, **offset;
2287 : MYSQL_RES *mysql_result;
2288 :
2289 0 : if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &result, &offset)==FAILURE) {
2290 0 : WRONG_PARAM_COUNT;
2291 : }
2292 :
2293 0 : ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
2294 :
2295 0 : convert_to_long_ex(offset);
2296 0 : if (Z_LVAL_PP(offset)<0 || Z_LVAL_PP(offset)>=(int)mysql_num_fields(mysql_result)) {
2297 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field %ld is invalid for MySQL result index %ld", Z_LVAL_PP(offset), Z_LVAL_PP(result));
2298 0 : RETURN_FALSE;
2299 : }
2300 0 : mysql_field_seek(mysql_result, Z_LVAL_PP(offset));
2301 0 : RETURN_TRUE;
2302 : }
2303 : /* }}} */
2304 :
2305 :
2306 : #define PHP_MYSQL_FIELD_NAME 1
2307 : #define PHP_MYSQL_FIELD_TABLE 2
2308 : #define PHP_MYSQL_FIELD_LEN 3
2309 : #define PHP_MYSQL_FIELD_TYPE 4
2310 : #define PHP_MYSQL_FIELD_FLAGS 5
2311 :
2312 : /* {{{ php_mysql_field_info
2313 : */
2314 : static void php_mysql_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
2315 0 : {
2316 : zval **result, **field;
2317 : MYSQL_RES *mysql_result;
2318 0 : MYSQL_FIELD *mysql_field = {0};
2319 : char buf[512];
2320 : int len;
2321 :
2322 0 : if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &result, &field)==FAILURE) {
2323 0 : WRONG_PARAM_COUNT;
2324 : }
2325 :
2326 0 : ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
2327 :
2328 0 : convert_to_long_ex(field);
2329 0 : if (Z_LVAL_PP(field)<0 || Z_LVAL_PP(field)>=(int)mysql_num_fields(mysql_result)) {
2330 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field %ld is invalid for MySQL result index %ld", Z_LVAL_PP(field), Z_LVAL_PP(result));
2331 0 : RETURN_FALSE;
2332 : }
2333 0 : mysql_field_seek(mysql_result, Z_LVAL_PP(field));
2334 0 : if ((mysql_field=mysql_fetch_field(mysql_result))==NULL) {
2335 0 : RETURN_FALSE;
2336 : }
2337 :
2338 0 : switch (entry_type) {
2339 : case PHP_MYSQL_FIELD_NAME:
2340 0 : Z_STRLEN_P(return_value) = strlen(mysql_field->name);
2341 0 : Z_STRVAL_P(return_value) = estrndup(mysql_field->name, Z_STRLEN_P(return_value));
2342 0 : Z_TYPE_P(return_value) = IS_STRING;
2343 0 : break;
2344 : case PHP_MYSQL_FIELD_TABLE:
2345 0 : Z_STRLEN_P(return_value) = strlen(mysql_field->table);
2346 0 : Z_STRVAL_P(return_value) = estrndup(mysql_field->table, Z_STRLEN_P(return_value));
2347 0 : Z_TYPE_P(return_value) = IS_STRING;
2348 0 : break;
2349 : case PHP_MYSQL_FIELD_LEN:
2350 0 : Z_LVAL_P(return_value) = mysql_field->length;
2351 0 : Z_TYPE_P(return_value) = IS_LONG;
2352 0 : break;
2353 : case PHP_MYSQL_FIELD_TYPE:
2354 0 : Z_STRVAL_P(return_value) = php_mysql_get_field_name(Z_TYPE_P(mysql_field));
2355 0 : Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
2356 0 : Z_STRVAL_P(return_value) = estrndup(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value));
2357 0 : Z_TYPE_P(return_value) = IS_STRING;
2358 0 : break;
2359 : case PHP_MYSQL_FIELD_FLAGS:
2360 0 : memcpy(buf, "", sizeof(""));
2361 : #ifdef IS_NOT_NULL
2362 0 : if (IS_NOT_NULL(mysql_field->flags)) {
2363 0 : strcat(buf, "not_null ");
2364 : }
2365 : #endif
2366 : #ifdef IS_PRI_KEY
2367 0 : if (IS_PRI_KEY(mysql_field->flags)) {
2368 0 : strcat(buf, "primary_key ");
2369 : }
2370 : #endif
2371 : #ifdef UNIQUE_KEY_FLAG
2372 0 : if (mysql_field->flags&UNIQUE_KEY_FLAG) {
2373 0 : strcat(buf, "unique_key ");
2374 : }
2375 : #endif
2376 : #ifdef MULTIPLE_KEY_FLAG
2377 0 : if (mysql_field->flags&MULTIPLE_KEY_FLAG) {
2378 0 : strcat(buf, "multiple_key ");
2379 : }
2380 : #endif
2381 : #ifdef IS_BLOB
2382 0 : if (IS_BLOB(mysql_field->flags)) {
2383 0 : strcat(buf, "blob ");
2384 : }
2385 : #endif
2386 : #ifdef UNSIGNED_FLAG
2387 0 : if (mysql_field->flags&UNSIGNED_FLAG) {
2388 0 : strcat(buf, "unsigned ");
2389 : }
2390 : #endif
2391 : #ifdef ZEROFILL_FLAG
2392 0 : if (mysql_field->flags&ZEROFILL_FLAG) {
2393 0 : strcat(buf, "zerofill ");
2394 : }
2395 : #endif
2396 : #ifdef BINARY_FLAG
2397 0 : if (mysql_field->flags&BINARY_FLAG) {
2398 0 : strcat(buf, "binary ");
2399 : }
2400 : #endif
2401 : #ifdef ENUM_FLAG
2402 0 : if (mysql_field->flags&ENUM_FLAG) {
2403 0 : strcat(buf, "enum ");
2404 : }
2405 : #endif
2406 : #ifdef SET_FLAG
2407 0 : if (mysql_field->flags&SET_FLAG) {
2408 0 : strcat(buf, "set ");
2409 : }
2410 : #endif
2411 : #ifdef AUTO_INCREMENT_FLAG
2412 0 : if (mysql_field->flags&AUTO_INCREMENT_FLAG) {
2413 0 : strcat(buf, "auto_increment ");
2414 : }
2415 : #endif
2416 : #ifdef TIMESTAMP_FLAG
2417 0 : if (mysql_field->flags&TIMESTAMP_FLAG) {
2418 0 : strcat(buf, "timestamp ");
2419 : }
2420 : #endif
2421 0 : len = strlen(buf);
2422 : /* remove trailing space, if present */
2423 0 : if (len && buf[len-1] == ' ') {
2424 0 : buf[len-1] = 0;
2425 0 : len--;
2426 : }
2427 :
2428 0 : Z_STRLEN_P(return_value) = len;
2429 0 : Z_STRVAL_P(return_value) = estrndup(buf, len);
2430 0 : Z_TYPE_P(return_value) = IS_STRING;
2431 0 : break;
2432 :
2433 : default:
2434 0 : RETURN_FALSE;
2435 : }
2436 : }
2437 : /* }}} */
2438 :
2439 : /* {{{ proto string mysql_field_name(resource result, int field_index)
2440 : Gets the name of the specified field in a result */
2441 : PHP_FUNCTION(mysql_field_name)
2442 0 : {
2443 0 : php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_NAME);
2444 0 : }
2445 : /* }}} */
2446 :
2447 :
2448 : /* {{{ proto string mysql_field_table(resource result, int field_offset)
2449 : Gets name of the table the specified field is in */
2450 : PHP_FUNCTION(mysql_field_table)
2451 0 : {
2452 0 : php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_TABLE);
2453 0 : }
2454 : /* }}} */
2455 :
2456 :
2457 : /* {{{ proto int mysql_field_len(resource result, int field_offset)
2458 : Returns the length of the specified field */
2459 : PHP_FUNCTION(mysql_field_len)
2460 0 : {
2461 0 : php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_LEN);
2462 0 : }
2463 : /* }}} */
2464 :
2465 :
2466 : /* {{{ proto string mysql_field_type(resource result, int field_offset)
2467 : Gets the type of the specified field in a result */
2468 : PHP_FUNCTION(mysql_field_type)
2469 0 : {
2470 0 : php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_TYPE);
2471 0 : }
2472 : /* }}} */
2473 :
2474 :
2475 : /* {{{ proto string mysql_field_flags(resource result, int field_offset)
2476 : Gets the flags associated with the specified field in a result */
2477 : PHP_FUNCTION(mysql_field_flags)
2478 0 : {
2479 0 : php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_FLAGS);
2480 0 : }
2481 : /* }}} */
2482 :
2483 :
2484 : /* {{{ proto bool mysql_free_result(resource result)
2485 : Free result memory */
2486 : PHP_FUNCTION(mysql_free_result)
2487 0 : {
2488 : zval **result;
2489 : MYSQL_RES *mysql_result;
2490 :
2491 0 : if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &result)==FAILURE) {
2492 0 : WRONG_PARAM_COUNT;
2493 : }
2494 :
2495 0 : if (Z_TYPE_PP(result)==IS_RESOURCE && Z_LVAL_PP(result)==0) {
2496 0 : RETURN_FALSE;
2497 : }
2498 :
2499 0 : ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
2500 :
2501 0 : zend_list_delete(Z_LVAL_PP(result));
2502 0 : RETURN_TRUE;
2503 : }
2504 : /* }}} */
2505 :
2506 : /* {{{ proto bool mysql_ping([int link_identifier])
2507 : Ping a server connection. If no connection then reconnect. */
2508 : PHP_FUNCTION(mysql_ping)
2509 0 : {
2510 0 : zval *mysql_link = NULL;
2511 0 : int id = -1;
2512 : php_mysql_conn *mysql;
2513 :
2514 0 : if (0 == ZEND_NUM_ARGS()) {
2515 0 : id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
2516 0 : CHECK_LINK(id);
2517 0 : } else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &mysql_link)==FAILURE) {
2518 0 : return;
2519 : }
2520 :
2521 0 : ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
2522 :
2523 0 : PHPMY_UNBUFFERED_QUERY_CHECK();
2524 :
2525 0 : RETURN_BOOL(! mysql_ping(&mysql->conn));
2526 : }
2527 : /* }}} */
2528 :
2529 : #endif
2530 :
2531 : /*
2532 : * Local variables:
2533 : * tab-width: 4
2534 : * c-basic-offset: 4
2535 : * End:
2536 : * vim600: sw=4 ts=4 fdm=marker
2537 : * vim<600: sw=4 ts=4
2538 : */
|