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