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 : | Author: Georg Richter <georg@php.net> |
16 : +----------------------------------------------------------------------+
17 :
18 : $Id: mysqli_api.c 288895 2009-09-28 13:11:21Z iliaa $
19 : */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : #include "config.h"
23 : #endif
24 :
25 : #include <signal.h>
26 :
27 : #include "php.h"
28 : #include "php_ini.h"
29 : #include "ext/standard/info.h"
30 : #include "php_mysqli.h"
31 :
32 : /* {{{ proto mixed mysqli_affected_rows(object link)
33 : Get number of affected rows in previous MySQL operation */
34 : PHP_FUNCTION(mysqli_affected_rows)
35 1 : {
36 : MY_MYSQL *mysql;
37 : zval *mysql_link;
38 : my_ulonglong rc;
39 :
40 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
41 0 : return;
42 : }
43 :
44 1 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
45 :
46 1 : rc = mysql_affected_rows(mysql->mysql);
47 1 : if (rc == (my_ulonglong) -1) {
48 0 : RETURN_LONG(-1);
49 : }
50 1 : MYSQLI_RETURN_LONG_LONG(rc);
51 : }
52 : /* }}} */
53 :
54 : /* {{{ proto bool mysqli_autocommit(object link, bool mode)
55 : Turn auto commit on or of */
56 : PHP_FUNCTION(mysqli_autocommit)
57 6 : {
58 : MY_MYSQL *mysql;
59 : zval *mysql_link;
60 : zend_bool automode;
61 :
62 6 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ob", &mysql_link, mysqli_link_class_entry, &automode) == FAILURE) {
63 0 : return;
64 : }
65 6 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
66 :
67 6 : if (mysql_autocommit(mysql->mysql, (my_bool)automode)) {
68 0 : RETURN_FALSE;
69 : }
70 6 : RETURN_TRUE;
71 : }
72 : /* }}} */
73 :
74 : /* {{{ proto bool mysqli_stmt_bind_param(object stmt, string types, mixed variable [,mixed,....])
75 : Bind variables to a prepared statement as parameters */
76 : PHP_FUNCTION(mysqli_stmt_bind_param)
77 14 : {
78 : zval ***args;
79 14 : int argc = ZEND_NUM_ARGS();
80 : int i;
81 : int num_vars;
82 14 : int start = 2;
83 : int ofs;
84 : MY_STMT *stmt;
85 : zval *mysql_stmt;
86 : MYSQL_BIND *bind;
87 : char *types;
88 : int typelen;
89 : unsigned long rc;
90 :
91 : /* calculate and check number of parameters */
92 14 : if (argc < 2) {
93 : /* there has to be at least one pair */
94 0 : WRONG_PARAM_COUNT;
95 : }
96 :
97 14 : if (zend_parse_method_parameters((getThis()) ? 1:2 TSRMLS_CC, getThis(), "Os", &mysql_stmt, mysqli_stmt_class_entry, &types, &typelen) == FAILURE) {
98 0 : return;
99 : }
100 :
101 14 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
102 :
103 14 : num_vars = argc - 1;
104 14 : if (getThis()) {
105 0 : start = 1;
106 : } else {
107 : /* ignore handle parameter in procedural interface*/
108 14 : --num_vars;
109 : }
110 :
111 14 : if (typelen != argc - start) {
112 : /* number of bind variables doesn't match number of elements in type definition string */
113 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of elements in type definition string doesn't match number of bind variables");
114 0 : RETURN_FALSE;
115 : }
116 :
117 14 : if (typelen != stmt->stmt->param_count) {
118 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of variables doesn't match number of parameters in prepared statement");
119 0 : RETURN_FALSE;
120 : }
121 :
122 : /* prevent leak if variables are already bound */
123 14 : if (stmt->param.var_cnt) {
124 1 : php_free_stmt_bind_buffer(stmt->param, FETCH_SIMPLE);
125 : }
126 :
127 14 : args = (zval ***)safe_emalloc(argc, sizeof(zval **), 0);
128 :
129 14 : if (zend_get_parameters_array_ex(argc, args) == FAILURE) {
130 0 : efree(args);
131 0 : WRONG_PARAM_COUNT;
132 : }
133 :
134 14 : stmt->param.is_null = ecalloc(num_vars, sizeof(char));
135 14 : bind = (MYSQL_BIND *)ecalloc(num_vars, sizeof(MYSQL_BIND));
136 :
137 14 : ofs = 0;
138 69 : for (i=start; i < argc; i++) {
139 :
140 : /* set specified type */
141 55 : switch (types[ofs]) {
142 : case 'd': /* Double */
143 1 : bind[ofs].buffer_type = MYSQL_TYPE_DOUBLE;
144 1 : bind[ofs].buffer = (char*)&Z_DVAL_PP(args[i]);
145 1 : bind[ofs].is_null = &stmt->param.is_null[ofs];
146 1 : break;
147 :
148 : case 'i': /* Integer */
149 34 : bind[ofs].buffer_type = MYSQL_TYPE_LONG;
150 34 : bind[ofs].buffer = (char*)&Z_LVAL_PP(args[i]);
151 34 : bind[ofs].is_null = &stmt->param.is_null[ofs];
152 34 : break;
153 :
154 : case 'b': /* Blob (send data) */
155 1 : bind[ofs].buffer_type = MYSQL_TYPE_LONG_BLOB;
156 : /* don't initialize is_null and length to 0 because we use ecalloc */
157 1 : break;
158 :
159 : case 's': /* string */
160 19 : bind[ofs].buffer_type = MYSQL_TYPE_VAR_STRING;
161 : /* don't initialize buffer and buffer_length because we use ecalloc */
162 19 : bind[ofs].is_null = &stmt->param.is_null[ofs];
163 19 : break;
164 :
165 : default:
166 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[ofs], i+1);
167 0 : RETVAL_FALSE;
168 0 : goto end;
169 : }
170 55 : ofs++;
171 : }
172 14 : rc = mysql_stmt_bind_param(stmt->stmt, bind);
173 14 : MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
174 :
175 14 : if (rc) {
176 0 : RETVAL_FALSE;
177 0 : goto end;
178 : }
179 :
180 14 : stmt->param.var_cnt = num_vars;
181 14 : stmt->param.vars = (zval **)safe_emalloc(num_vars, sizeof(zval), 0);
182 69 : for (i = 0; i < num_vars; i++) {
183 55 : if (bind[i].buffer_type != MYSQL_TYPE_LONG_BLOB) {
184 54 : ZVAL_ADDREF(*args[i+start]);
185 54 : stmt->param.vars[i] = *args[i+start];
186 : } else {
187 1 : stmt->param.vars[i] = NULL;
188 : }
189 : }
190 14 : RETVAL_TRUE;
191 14 : end:
192 14 : efree(args);
193 14 : efree(bind);
194 : }
195 : /* }}} */
196 :
197 : /* {{{ proto bool mysqli_stmt_bind_result(object stmt, mixed var, [,mixed, ...])
198 : Bind variables to a prepared statement for result storage */
199 :
200 : /* TODO:
201 : do_alloca, free_alloca
202 : */
203 :
204 : PHP_FUNCTION(mysqli_stmt_bind_result)
205 42 : {
206 : zval ***args;
207 42 : int argc = ZEND_NUM_ARGS();
208 : int i;
209 42 : int start = 1;
210 : int var_cnt;
211 : int ofs;
212 : long col_type;
213 : ulong rc;
214 : MY_STMT *stmt;
215 : zval *mysql_stmt;
216 : MYSQL_BIND *bind;
217 :
218 42 : if (getThis()) {
219 14 : start = 0;
220 : }
221 :
222 42 : if (zend_parse_method_parameters((getThis()) ? 0:1 TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
223 0 : return;
224 : }
225 :
226 42 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
227 :
228 42 : if (argc < (getThis() ? 1 : 2)) {
229 0 : WRONG_PARAM_COUNT;
230 : }
231 :
232 42 : args = (zval ***)safe_emalloc(argc, sizeof(zval **), 0);
233 :
234 42 : if (zend_get_parameters_array_ex(argc, args) == FAILURE) {
235 0 : efree(args);
236 0 : WRONG_PARAM_COUNT;
237 : }
238 :
239 42 : var_cnt = argc - start;
240 :
241 42 : if (var_cnt != mysql_stmt_field_count(stmt->stmt)) {
242 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of bind variables doesn't match number of fields in prepared statement");
243 0 : efree(args);
244 0 : RETURN_FALSE;
245 : }
246 :
247 : /* prevent leak if variables are already bound */
248 42 : if (stmt->result.var_cnt) {
249 1 : php_free_stmt_bind_buffer(stmt->result, FETCH_RESULT);
250 : }
251 :
252 42 : bind = (MYSQL_BIND *)ecalloc(var_cnt, sizeof(MYSQL_BIND));
253 : {
254 : int size;
255 42 : char *p= emalloc(size= var_cnt * (sizeof(char) + sizeof(VAR_BUFFER)));
256 42 : stmt->result.buf = (VAR_BUFFER *) p;
257 42 : stmt->result.is_null = p + var_cnt * sizeof(VAR_BUFFER);
258 42 : memset(p, 0, size);
259 : }
260 :
261 1206 : for (i=start; i < var_cnt + start ; i++) {
262 1164 : ofs = i - start;
263 1164 : col_type = (stmt->stmt->fields) ? stmt->stmt->fields[ofs].type : MYSQL_TYPE_STRING;
264 :
265 1164 : switch (col_type) {
266 : case MYSQL_TYPE_DOUBLE:
267 : case MYSQL_TYPE_FLOAT:
268 18 : convert_to_double_ex(args[i]);
269 18 : stmt->result.buf[ofs].type = IS_DOUBLE;
270 18 : stmt->result.buf[ofs].buflen = sizeof(double);
271 :
272 : /* allocate buffer for double */
273 18 : stmt->result.buf[ofs].val = (char *)emalloc(sizeof(double));
274 18 : bind[ofs].buffer_type = MYSQL_TYPE_DOUBLE;
275 18 : bind[ofs].buffer = stmt->result.buf[ofs].val;
276 18 : bind[ofs].is_null = &stmt->result.is_null[ofs];
277 18 : break;
278 :
279 : case MYSQL_TYPE_NULL:
280 1 : stmt->result.buf[ofs].type = IS_NULL;
281 : /*
282 : don't initialize to 0 :
283 : 1. stmt->result.buf[ofs].buflen
284 : 2. bind[ofs].buffer
285 : 3. bind[ofs].buffer_length
286 : because memory was allocated with ecalloc
287 : */
288 1 : bind[ofs].buffer_type = MYSQL_TYPE_NULL;
289 1 : bind[ofs].is_null = &stmt->result.is_null[ofs];
290 1 : break;
291 :
292 : case MYSQL_TYPE_SHORT:
293 : case MYSQL_TYPE_TINY:
294 : case MYSQL_TYPE_LONG:
295 : case MYSQL_TYPE_INT24:
296 : case MYSQL_TYPE_YEAR:
297 71 : convert_to_long_ex(args[i]);
298 71 : stmt->result.buf[ofs].type = IS_LONG;
299 : /* don't set stmt->result.buf[ofs].buflen to 0, we used ecalloc */
300 71 : stmt->result.buf[ofs].val = (char *)emalloc(sizeof(int));
301 71 : bind[ofs].buffer_type = MYSQL_TYPE_LONG;
302 71 : bind[ofs].buffer = stmt->result.buf[ofs].val;
303 71 : bind[ofs].is_null = &stmt->result.is_null[ofs];
304 71 : bind[ofs].is_unsigned = (stmt->stmt->fields[ofs].flags & UNSIGNED_FLAG) ? 1 : 0;
305 71 : break;
306 :
307 : case MYSQL_TYPE_LONGLONG:
308 : #if MYSQL_VERSION_ID > 50002
309 : case MYSQL_TYPE_BIT:
310 : #endif
311 16 : stmt->result.buf[ofs].type = IS_STRING;
312 16 : stmt->result.buf[ofs].buflen = sizeof(my_ulonglong);
313 16 : stmt->result.buf[ofs].val = (char *)emalloc(stmt->result.buf[ofs].buflen);
314 16 : bind[ofs].buffer_type = col_type;
315 16 : bind[ofs].buffer = stmt->result.buf[ofs].val;
316 16 : bind[ofs].is_null = &stmt->result.is_null[ofs];
317 16 : bind[ofs].buffer_length = stmt->result.buf[ofs].buflen;
318 16 : bind[ofs].is_unsigned = (stmt->stmt->fields[ofs].flags & UNSIGNED_FLAG) ? 1 : 0;
319 16 : break;
320 :
321 : case MYSQL_TYPE_DATE:
322 : case MYSQL_TYPE_TIME:
323 : case MYSQL_TYPE_DATETIME:
324 : case MYSQL_TYPE_NEWDATE:
325 : case MYSQL_TYPE_VAR_STRING:
326 : case MYSQL_TYPE_STRING:
327 : case MYSQL_TYPE_BLOB:
328 : case MYSQL_TYPE_TINY_BLOB:
329 : case MYSQL_TYPE_MEDIUM_BLOB:
330 : case MYSQL_TYPE_LONG_BLOB:
331 : case MYSQL_TYPE_TIMESTAMP:
332 : case MYSQL_TYPE_DECIMAL:
333 : #ifdef FIELD_TYPE_NEWDECIMAL
334 : case MYSQL_TYPE_NEWDECIMAL:
335 : #endif
336 : {
337 : #if MYSQL_VERSION_ID > 50099
338 : /* Changed to my_bool in MySQL 5.1. See MySQL Bug #16144 */
339 : my_bool tmp;
340 : #else
341 1058 : ulong tmp = 0;
342 : #endif
343 1058 : stmt->result.buf[ofs].type = IS_STRING;
344 : /*
345 : If the user has called $stmt->store_result() then we have asked
346 : max_length to be updated. this is done only for BLOBS because we don't want to allocate
347 : big chunkgs of memory 2^16 or 2^24
348 : */
349 1116 : if (stmt->stmt->fields[ofs].max_length == 0 &&
350 : !mysql_stmt_attr_get(stmt->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &tmp) && !tmp)
351 : {
352 58 : stmt->result.buf[ofs].buflen =
353 : (stmt->stmt->fields) ? (stmt->stmt->fields[ofs].length) ? stmt->stmt->fields[ofs].length + 1: 256: 256;
354 : } else {
355 : /*
356 : the user has called store_result(). if he does not there is no way to determine the
357 : libmysql does not allow us to allocate 0 bytes for a buffer so we try 1
358 : */
359 1000 : if (!(stmt->result.buf[ofs].buflen = stmt->stmt->fields[ofs].max_length))
360 1000 : ++stmt->result.buf[ofs].buflen;
361 : }
362 1058 : stmt->result.buf[ofs].val = (char *)emalloc(stmt->result.buf[ofs].buflen);
363 1058 : bind[ofs].buffer_type = MYSQL_TYPE_STRING;
364 1058 : bind[ofs].buffer = stmt->result.buf[ofs].val;
365 1058 : bind[ofs].is_null = &stmt->result.is_null[ofs];
366 1058 : bind[ofs].buffer_length = stmt->result.buf[ofs].buflen;
367 1058 : bind[ofs].length = &stmt->result.buf[ofs].output_len;
368 1058 : break;
369 : }
370 : default:
371 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Server returned unknown type %ld. Probably your client library is incompatible with the server version you use!", col_type);
372 : break;
373 : }
374 : }
375 :
376 42 : rc = mysql_stmt_bind_result(stmt->stmt, bind);
377 42 : MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
378 :
379 42 : if (rc) {
380 : /* dont close the statement or subsequent usage (for example ->execute()) will lead to crash */
381 0 : for (i=0; i < var_cnt ; i++) {
382 0 : if (stmt->result.buf[i].val) {
383 0 : efree(stmt->result.buf[i].val);
384 : }
385 : }
386 : /* Don't free stmt->result.is_null because is_null & buf are one block of memory */
387 0 : efree(stmt->result.buf);
388 0 : RETVAL_FALSE;
389 : } else {
390 42 : stmt->result.var_cnt = var_cnt;
391 42 : stmt->result.vars = (zval **)safe_emalloc((var_cnt), sizeof(zval), 0);
392 1206 : for (i = start; i < var_cnt+start; i++) {
393 1164 : ofs = i-start;
394 1164 : ZVAL_ADDREF(*args[i]);
395 1164 : stmt->result.vars[ofs] = *args[i];
396 : }
397 42 : RETVAL_TRUE;
398 : }
399 42 : efree(args);
400 42 : efree(bind);
401 : }
402 : /* }}} */
403 :
404 : /* {{{ proto bool mysqli_change_user(object link, string user, string password, string database)
405 : Change logged-in user of the active connection */
406 : PHP_FUNCTION(mysqli_change_user)
407 0 : {
408 : MY_MYSQL *mysql;
409 0 : zval *mysql_link = NULL;
410 : char *user, *password, *dbname;
411 : int user_len, password_len, dbname_len;
412 : ulong rc;
413 :
414 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osss", &mysql_link, mysqli_link_class_entry, &user, &user_len, &password, &password_len, &dbname, &dbname_len) == FAILURE) {
415 0 : return;
416 : }
417 0 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
418 :
419 0 : rc = mysql_change_user(mysql->mysql, user, password, dbname);
420 0 : MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
421 :
422 0 : if (rc) {
423 0 : RETURN_FALSE;
424 : }
425 :
426 0 : RETURN_TRUE;
427 : }
428 : /* }}} */
429 :
430 : /* {{{ proto string mysqli_character_set_name(object link)
431 : Returns the name of the character set used for this connection */
432 : PHP_FUNCTION(mysqli_character_set_name)
433 4 : {
434 : MY_MYSQL *mysql;
435 : zval *mysql_link;
436 :
437 4 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
438 0 : return;
439 : }
440 :
441 4 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
442 :
443 4 : RETURN_STRING((char *) mysql_character_set_name(mysql->mysql), 1);
444 : }
445 : /* }}} */
446 :
447 : /* {{{ proto bool mysqli_close(object link)
448 : Close connection */
449 : PHP_FUNCTION(mysqli_close)
450 93 : {
451 : zval *mysql_link;
452 : MY_MYSQL *mysql;
453 :
454 93 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
455 0 : return;
456 : }
457 :
458 93 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_INITIALIZED);
459 :
460 93 : mysql_close(mysql->mysql);
461 93 : mysql->mysql = NULL;
462 93 : php_clear_mysql(mysql);
463 93 : efree(mysql);
464 93 : MYSQLI_CLEAR_RESOURCE(&mysql_link);
465 93 : RETURN_TRUE;
466 : }
467 : /* }}} */
468 :
469 : /* {{{ proto bool mysqli_commit(object link)
470 : Commit outstanding actions and close transaction */
471 : PHP_FUNCTION(mysqli_commit)
472 2 : {
473 : MY_MYSQL *mysql;
474 : zval *mysql_link;
475 :
476 2 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
477 0 : return;
478 : }
479 2 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
480 2 : if (mysql_commit(mysql->mysql)) {
481 0 : RETURN_FALSE;
482 : }
483 2 : RETURN_TRUE;
484 : }
485 : /* }}} */
486 :
487 : /* {{{ proto bool mysqli_data_seek(object result, int offset)
488 : Move internal result pointer */
489 : PHP_FUNCTION(mysqli_data_seek)
490 0 : {
491 : MYSQL_RES *result;
492 : zval *mysql_result;
493 : long offset;
494 :
495 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &mysql_result, mysqli_result_class_entry, &offset) == FAILURE) {
496 0 : return;
497 : }
498 :
499 0 : MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
500 :
501 0 : if (result->handle && result->handle->status == MYSQL_STATUS_USE_RESULT) {
502 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Function cannot be used with MYSQL_USE_RESULT");
503 0 : RETURN_FALSE;
504 : }
505 :
506 0 : if (offset < 0 || offset >= result->row_count) {
507 0 : RETURN_FALSE;
508 : }
509 :
510 0 : mysql_data_seek(result, offset);
511 0 : RETURN_TRUE;
512 : }
513 : /* }}} */
514 :
515 : /* {{{ proto void mysqli_debug(string debug)
516 : */
517 : PHP_FUNCTION(mysqli_debug)
518 0 : {
519 : char *debug;
520 : int debug_len;
521 :
522 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &debug, &debug_len) == FAILURE) {
523 0 : return;
524 : }
525 :
526 0 : mysql_debug(debug);
527 0 : RETURN_TRUE;
528 : }
529 : /* }}} */
530 :
531 : /* {{{ proto bool mysqli_dump_debug_info(object link)
532 : */
533 : PHP_FUNCTION(mysqli_dump_debug_info)
534 0 : {
535 : MY_MYSQL *mysql;
536 : zval *mysql_link;
537 : ulong rc;
538 :
539 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
540 0 : return;
541 : }
542 0 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
543 :
544 0 : rc = mysql_dump_debug_info(mysql->mysql);
545 :
546 0 : if (rc) {
547 0 : RETURN_FALSE;
548 : }
549 0 : RETURN_TRUE;
550 : }
551 : /* }}} */
552 :
553 : /* {{{ proto int mysqli_errno(object link)
554 : Returns the numerical value of the error message from previous MySQL operation */
555 : PHP_FUNCTION(mysqli_errno)
556 3 : {
557 : MY_MYSQL *mysql;
558 : zval *mysql_link;
559 :
560 3 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
561 0 : return;
562 : }
563 3 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
564 3 : RETURN_LONG(mysql_errno(mysql->mysql));
565 : }
566 : /* }}} */
567 :
568 : /* {{{ proto string mysqli_error(object link)
569 : Returns the text of the error message from previous MySQL operation */
570 : PHP_FUNCTION(mysqli_error)
571 2 : {
572 : MY_MYSQL *mysql;
573 : zval *mysql_link;
574 :
575 2 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
576 0 : return;
577 : }
578 2 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
579 2 : RETURN_STRING((char *)mysql_error(mysql->mysql),1);
580 : }
581 : /* }}} */
582 :
583 : /* {{{ proto bool mysqli_stmt_execute(object stmt)
584 : Execute a prepared statement */
585 : PHP_FUNCTION(mysqli_stmt_execute)
586 62 : {
587 : MY_STMT *stmt;
588 : zval *mysql_stmt;
589 : unsigned int i;
590 :
591 62 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
592 0 : return;
593 : }
594 62 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
595 :
596 117 : for (i = 0; i < stmt->param.var_cnt; i++) {
597 55 : if (stmt->param.vars[i]) {
598 54 : if ( !(stmt->param.is_null[i] = (stmt->param.vars[i]->type == IS_NULL)) ) {
599 51 : switch (stmt->stmt->params[i].buffer_type) {
600 : case MYSQL_TYPE_VAR_STRING:
601 19 : convert_to_string_ex(&stmt->param.vars[i]);
602 19 : stmt->stmt->params[i].buffer = Z_STRVAL_PP(&stmt->param.vars[i]);
603 19 : stmt->stmt->params[i].buffer_length = Z_STRLEN_PP(&stmt->param.vars[i]);
604 19 : break;
605 : case MYSQL_TYPE_DOUBLE:
606 1 : convert_to_double_ex(&stmt->param.vars[i]);
607 1 : stmt->stmt->params[i].buffer = (char*)&Z_LVAL_PP(&stmt->param.vars[i]);
608 1 : break;
609 : case MYSQL_TYPE_LONG:
610 31 : convert_to_long_ex(&stmt->param.vars[i]);
611 31 : stmt->stmt->params[i].buffer = (char*)&Z_LVAL_PP(&stmt->param.vars[i]);
612 : break;
613 : default:
614 : break;
615 : }
616 : }
617 : }
618 : }
619 62 : if (mysql_stmt_execute(stmt->stmt)) {
620 1 : MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
621 1 : RETURN_FALSE;
622 : }
623 :
624 61 : if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
625 0 : php_mysqli_report_index(stmt->query, stmt->stmt->mysql->server_status TSRMLS_CC);
626 : }
627 :
628 61 : RETURN_TRUE;
629 : }
630 : /* }}} */
631 :
632 : /* {{{ proto mixed mysqli_stmt_fetch(object stmt)
633 : Fetch results from a prepared statement into the bound variables */
634 : PHP_FUNCTION(mysqli_stmt_fetch)
635 82 : {
636 : MY_STMT *stmt;
637 : zval *mysql_stmt;
638 : unsigned int i;
639 : ulong ret;
640 : unsigned int uval;
641 : my_ulonglong llval;
642 :
643 :
644 82 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
645 0 : return;
646 : }
647 82 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
648 :
649 : /* reset buffers */
650 :
651 :
652 1300 : for (i = 0; i < stmt->result.var_cnt; i++) {
653 1218 : if (stmt->result.buf[i].type == IS_STRING) {
654 1100 : memset(stmt->result.buf[i].val, 0, stmt->result.buf[i].buflen);
655 : }
656 : }
657 82 : ret = mysql_stmt_fetch(stmt->stmt);
658 : #ifdef MYSQL_DATA_TRUNCATED
659 160 : if (!ret || ret == MYSQL_DATA_TRUNCATED) {
660 : #else
661 : if (!ret) {
662 : #endif
663 1284 : for (i = 0; i < stmt->result.var_cnt; i++) {
664 : /* Even if the string is of length zero there is one byte alloced so efree() in all cases */
665 1206 : if (Z_TYPE_P(stmt->result.vars[i]) == IS_STRING) {
666 1020 : efree(stmt->result.vars[i]->value.str.val);
667 : }
668 1206 : if (!stmt->result.is_null[i]) {
669 1178 : switch (stmt->result.buf[i].type) {
670 : case IS_LONG:
671 82 : if ((stmt->stmt->fields[i].type == MYSQL_TYPE_LONG)
672 : && (stmt->stmt->fields[i].flags & UNSIGNED_FLAG))
673 : {
674 : /* unsigned int (11) */
675 16 : uval= *(unsigned int *) stmt->result.buf[i].val;
676 :
677 16 : if (uval > INT_MAX) {
678 : char *tmp, *p;
679 5 : int j=10;
680 5 : tmp= emalloc(11);
681 5 : p= &tmp[9];
682 : do {
683 50 : *p-- = (uval % 10) + 48;
684 50 : uval = uval / 10;
685 50 : } while (--j > 0);
686 5 : tmp[10]= '\0';
687 : /* unsigned int > INT_MAX is 10 digis - ALWAYS */
688 5 : ZVAL_STRINGL(stmt->result.vars[i], tmp, 10, 0);
689 5 : break;
690 : }
691 : }
692 77 : if (stmt->stmt->fields[i].flags & UNSIGNED_FLAG) {
693 23 : ZVAL_LONG(stmt->result.vars[i], *(unsigned int *)stmt->result.buf[i].val);
694 : } else {
695 54 : ZVAL_LONG(stmt->result.vars[i], *(int *)stmt->result.buf[i].val);
696 : }
697 77 : break;
698 : case IS_DOUBLE:
699 13 : ZVAL_DOUBLE(stmt->result.vars[i], *(double *)stmt->result.buf[i].val);
700 13 : break;
701 : case IS_STRING:
702 1083 : if (stmt->stmt->bind[i].buffer_type == MYSQL_TYPE_LONGLONG) {
703 25 : my_bool uns= (stmt->stmt->fields[i].flags & UNSIGNED_FLAG)? 1:0;
704 25 : llval= *(my_ulonglong *) stmt->result.buf[i].val;
705 : #if SIZEOF_LONG==8
706 : if (uns && llval > 9223372036854775807L) {
707 : #elif SIZEOF_LONG==4
708 36 : if ((uns && llval > L64(2147483647)) ||
709 : (!uns && (( L64(2147483647) < (my_longlong) llval) || (L64(-2147483648) > (my_longlong) llval))))
710 : {
711 : #endif
712 : char tmp[22];
713 : /* even though lval is declared as unsigned, the value
714 : * may be negative. Therefor we cannot use MYSQLI_LLU_SPEC and must
715 : * use MYSQLI_LL_SPEC.
716 : */
717 11 : snprintf(tmp, sizeof(tmp), (stmt->stmt->fields[i].flags & UNSIGNED_FLAG)? MYSQLI_LLU_SPEC : MYSQLI_LL_SPEC, llval);
718 11 : ZVAL_STRING(stmt->result.vars[i], tmp, 1);
719 : } else {
720 14 : ZVAL_LONG(stmt->result.vars[i], llval);
721 : }
722 : }
723 : #if MYSQL_VERSION_ID > 50002
724 1058 : else if (stmt->stmt->bind[i].buffer_type == MYSQL_TYPE_BIT) {
725 0 : llval = *(my_ulonglong *)stmt->result.buf[i].val;
726 0 : ZVAL_LONG(stmt->result.vars[i], llval);
727 : }
728 : #endif
729 : else {
730 : #if defined(MYSQL_DATA_TRUNCATED) && MYSQL_VERSION_ID > 50002
731 1059 : if(ret == MYSQL_DATA_TRUNCATED && *(stmt->stmt->bind[i].error) != 0) {
732 : /* result was truncated */
733 1 : ZVAL_STRINGL(stmt->result.vars[i], stmt->result.buf[i].val, stmt->stmt->bind[i].buffer_length, 1);
734 : } else {
735 : #else
736 : {
737 : #endif
738 1057 : ZVAL_STRINGL(stmt->result.vars[i], stmt->result.buf[i].val, stmt->result.buf[i].output_len, 1);
739 : }
740 : }
741 : break;
742 : default:
743 : break;
744 : }
745 : } else {
746 28 : ZVAL_NULL(stmt->result.vars[i]);
747 : }
748 : }
749 : } else {
750 4 : MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
751 : }
752 :
753 82 : switch (ret) {
754 : case 0:
755 : #ifdef MYSQL_DATA_TRUNCATED
756 : /* according to SQL standard truncation (e.g. loss of precision is
757 : not an error) - for detecting possible truncation you have to
758 : check mysqli_stmt_warning
759 : */
760 : case MYSQL_DATA_TRUNCATED:
761 : #endif
762 78 : RETURN_TRUE;
763 : break;
764 : case 1:
765 0 : RETURN_FALSE;
766 : break;
767 : default:
768 4 : RETURN_NULL();
769 : break;
770 : }
771 : }
772 : /* }}} */
773 :
774 : /* {{{ proto mixed mysqli_fetch_field (object result)
775 : Get column information from a result and return as an object */
776 : PHP_FUNCTION(mysqli_fetch_field)
777 3 : {
778 : MYSQL_RES *result;
779 : zval *mysql_result;
780 : MYSQL_FIELD *field;
781 :
782 3 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
783 0 : return;
784 : }
785 :
786 3 : MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
787 :
788 3 : if (!(field = mysql_fetch_field(result))) {
789 1 : RETURN_FALSE;
790 : }
791 :
792 2 : object_init(return_value);
793 :
794 2 : add_property_string(return_value, "name",(field->name ? field->name : ""), 1);
795 2 : add_property_string(return_value, "orgname",(field->org_name ? field->org_name : ""), 1);
796 2 : add_property_string(return_value, "table",(field->table ? field->table : ""), 1);
797 2 : add_property_string(return_value, "orgtable",(field->org_table ? field->org_table : ""), 1);
798 2 : add_property_string(return_value, "def",(field->def ? field->def : ""), 1);
799 2 : add_property_long(return_value, "max_length", field->max_length);
800 2 : add_property_long(return_value, "length", field->length);
801 2 : add_property_long(return_value, "charsetnr", field->charsetnr);
802 2 : add_property_long(return_value, "flags", field->flags);
803 2 : add_property_long(return_value, "type", field->type);
804 2 : add_property_long(return_value, "decimals", field->decimals);
805 : }
806 : /* }}} */
807 :
808 : /* {{{ proto mixed mysqli_fetch_fields (object result)
809 : Return array of objects containing field meta-data */
810 : PHP_FUNCTION(mysqli_fetch_fields)
811 1 : {
812 : MYSQL_RES *result;
813 : zval *mysql_result;
814 : MYSQL_FIELD *field;
815 : zval *obj;
816 :
817 : unsigned int i;
818 :
819 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
820 0 : return;
821 : }
822 :
823 1 : MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
824 :
825 1 : array_init(return_value);
826 :
827 3 : for (i = 0; i < mysql_num_fields(result); i++) {
828 2 : field = mysql_fetch_field_direct(result, i);
829 :
830 :
831 2 : MAKE_STD_ZVAL(obj);
832 2 : object_init(obj);
833 :
834 2 : add_property_string(obj, "name",(field->name ? field->name : ""), 1);
835 2 : add_property_string(obj, "orgname",(field->org_name ? field->org_name : ""), 1);
836 2 : add_property_string(obj, "table",(field->table ? field->table : ""), 1);
837 2 : add_property_string(obj, "orgtable",(field->org_table ? field->org_table : ""), 1);
838 2 : add_property_string(obj, "def",(field->def ? field->def : ""), 1);
839 2 : add_property_long(obj, "max_length", field->max_length);
840 2 : add_property_long(obj, "length", field->length);
841 2 : add_property_long(obj, "charsetnr", field->charsetnr);
842 2 : add_property_long(obj, "flags", field->flags);
843 2 : add_property_long(obj, "type", field->type);
844 2 : add_property_long(obj, "decimals", field->decimals);
845 :
846 2 : add_index_zval(return_value, i, obj);
847 : }
848 : }
849 : /* }}} */
850 :
851 : /* {{{ proto mixed mysqli_fetch_field_direct (object result, int offset)
852 : Fetch meta-data for a single field */
853 : PHP_FUNCTION(mysqli_fetch_field_direct)
854 2 : {
855 : MYSQL_RES *result;
856 : zval *mysql_result;
857 : MYSQL_FIELD *field;
858 : long offset;
859 :
860 2 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &mysql_result, mysqli_result_class_entry, &offset) == FAILURE) {
861 0 : return;
862 : }
863 :
864 2 : MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
865 :
866 2 : if (offset < 0 || offset >= mysql_num_fields(result)) {
867 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field offset is invalid for resultset");
868 0 : RETURN_FALSE;
869 : }
870 :
871 2 : if (!(field = mysql_fetch_field_direct(result,offset))) {
872 0 : RETURN_FALSE;
873 : }
874 :
875 2 : object_init(return_value);
876 :
877 2 : add_property_string(return_value, "name",(field->name ? field->name : ""), 1);
878 2 : add_property_string(return_value, "orgname",(field->org_name ? field->org_name : ""), 1);
879 2 : add_property_string(return_value, "table",(field->table ? field->table : ""), 1);
880 2 : add_property_string(return_value, "orgtable",(field->org_table ? field->org_table : ""), 1);
881 2 : add_property_string(return_value, "def",(field->def ? field->def : ""), 1);
882 2 : add_property_long(return_value, "max_length", field->max_length);
883 2 : add_property_long(return_value, "length", field->length);
884 2 : add_property_long(return_value, "charsetnr", field->charsetnr);
885 2 : add_property_long(return_value, "flags", field->flags);
886 2 : add_property_long(return_value, "type", field->type);
887 2 : add_property_long(return_value, "decimals", field->decimals);
888 : }
889 : /* }}} */
890 :
891 : /* {{{ proto mixed mysqli_fetch_lengths (object result)
892 : Get the length of each output in a result */
893 : PHP_FUNCTION(mysqli_fetch_lengths)
894 1 : {
895 : MYSQL_RES *result;
896 : zval *mysql_result;
897 : unsigned int i;
898 : unsigned long *ret;
899 :
900 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
901 0 : return;
902 : }
903 :
904 1 : MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
905 :
906 1 : if (!(ret = mysql_fetch_lengths(result))) {
907 1 : RETURN_FALSE;
908 : }
909 :
910 0 : array_init(return_value);
911 :
912 0 : for (i = 0; i < mysql_num_fields(result); i++) {
913 0 : add_index_long(return_value, i, ret[i]);
914 : }
915 : }
916 : /* }}} */
917 :
918 : /* {{{ proto array mysqli_fetch_row (object result)
919 : Get a result row as an enumerated array */
920 : PHP_FUNCTION(mysqli_fetch_row)
921 20 : {
922 20 : php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_NUM, 0);
923 20 : }
924 : /* }}} */
925 :
926 : /* {{{ proto int mysqli_field_count(object link)
927 : Fetch the number of fields returned by the last query for the given link
928 : */
929 : PHP_FUNCTION(mysqli_field_count)
930 5 : {
931 : MY_MYSQL *mysql;
932 : zval *mysql_link;
933 :
934 5 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
935 0 : return;
936 : }
937 5 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
938 :
939 5 : RETURN_LONG(mysql_field_count(mysql->mysql));
940 : }
941 : /* }}} */
942 :
943 : /* {{{ proto int mysqli_field_seek(object result, int fieldnr)
944 : Set result pointer to a specified field offset
945 : */
946 : PHP_FUNCTION(mysqli_field_seek)
947 0 : {
948 : MYSQL_RES *result;
949 : zval *mysql_result;
950 : unsigned long fieldnr;
951 :
952 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &mysql_result, mysqli_result_class_entry, &fieldnr) == FAILURE) {
953 0 : return;
954 : }
955 0 : MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
956 :
957 0 : if (fieldnr < 0 || fieldnr >= mysql_num_fields(result)) {
958 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid field offset");
959 0 : RETURN_FALSE;
960 : }
961 :
962 0 : mysql_field_seek(result, fieldnr);
963 0 : RETURN_TRUE;
964 : }
965 : /* }}} */
966 :
967 : /* {{{ proto int mysqli_field_tell(object result)
968 : Get current field offset of result pointer */
969 : PHP_FUNCTION(mysqli_field_tell)
970 0 : {
971 : MYSQL_RES *result;
972 : zval *mysql_result;
973 :
974 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
975 0 : return;
976 : }
977 0 : MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
978 :
979 0 : RETURN_LONG(mysql_field_tell(result));
980 : }
981 : /* }}} */
982 :
983 : /* {{{ proto void mysqli_free_result(object result)
984 : Free query result memory for the given result handle */
985 : PHP_FUNCTION(mysqli_free_result)
986 33 : {
987 : MYSQL_RES *result;
988 : zval *mysql_result;
989 :
990 33 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
991 0 : return;
992 : }
993 33 : MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
994 :
995 33 : mysql_free_result(result);
996 33 : MYSQLI_CLEAR_RESOURCE(&mysql_result);
997 : }
998 : /* }}} */
999 :
1000 : /* {{{ proto string mysqli_get_client_info(void)
1001 : Get MySQL client info */
1002 : PHP_FUNCTION(mysqli_get_client_info)
1003 0 : {
1004 0 : RETURN_STRING((char *)mysql_get_client_info(), 1);
1005 : }
1006 : /* }}} */
1007 :
1008 : /* {{{ proto int mysqli_get_client_version(void)
1009 : Get MySQL client info */
1010 : PHP_FUNCTION(mysqli_get_client_version)
1011 1 : {
1012 1 : RETURN_LONG((long)mysql_get_client_version());
1013 : }
1014 : /* }}} */
1015 :
1016 : /* {{{ proto string mysqli_get_host_info (object link)
1017 : Get MySQL host info */
1018 : PHP_FUNCTION(mysqli_get_host_info)
1019 1 : {
1020 : MY_MYSQL *mysql;
1021 1 : zval *mysql_link = NULL;
1022 :
1023 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1024 0 : return;
1025 : }
1026 1 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1027 :
1028 1 : RETURN_STRING((mysql->mysql->host_info) ? mysql->mysql->host_info : "", 1);
1029 : }
1030 : /* }}} */
1031 :
1032 : /* {{{ proto int mysqli_get_proto_info(object link)
1033 : Get MySQL protocol information */
1034 : PHP_FUNCTION(mysqli_get_proto_info)
1035 1 : {
1036 : MY_MYSQL *mysql;
1037 1 : zval *mysql_link = NULL;
1038 :
1039 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1040 0 : return;
1041 : }
1042 1 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1043 :
1044 1 : RETURN_LONG(mysql_get_proto_info(mysql->mysql));
1045 : }
1046 : /* }}} */
1047 :
1048 : /* {{{ proto string mysqli_get_server_info(object link)
1049 : Get MySQL server info */
1050 : PHP_FUNCTION(mysqli_get_server_info)
1051 1 : {
1052 : MY_MYSQL *mysql;
1053 1 : zval *mysql_link = NULL;
1054 :
1055 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1056 0 : return;
1057 : }
1058 1 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1059 :
1060 1 : RETURN_STRING((char *)mysql_get_server_info(mysql->mysql), 1);
1061 : }
1062 :
1063 : /* }}} */
1064 :
1065 : /* {{{ proto int mysqli_get_server_version(object link)
1066 : Return the MySQL version for the server referenced by the given link */
1067 : PHP_FUNCTION(mysqli_get_server_version)
1068 4 : {
1069 : MY_MYSQL *mysql;
1070 4 : zval *mysql_link = NULL;
1071 :
1072 4 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1073 0 : return;
1074 : }
1075 4 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1076 :
1077 4 : RETURN_LONG(mysql_get_server_version(mysql->mysql));
1078 : }
1079 : /* }}} */
1080 :
1081 : /* {{{ proto string mysqli_info(object link)
1082 : Get information about the most recent query */
1083 : PHP_FUNCTION(mysqli_info)
1084 1 : {
1085 : MY_MYSQL *mysql;
1086 1 : zval *mysql_link = NULL;
1087 :
1088 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1089 0 : return;
1090 : }
1091 1 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1092 :
1093 1 : RETURN_STRING((mysql->mysql->info) ? mysql->mysql->info : "", 1);
1094 : }
1095 : /* }}} */
1096 :
1097 : /* {{{ proto resource mysqli_init(void)
1098 : Initialize mysqli and return a resource for use with mysql_real_connect */
1099 : PHP_FUNCTION(mysqli_init)
1100 11 : {
1101 : MYSQLI_RESOURCE *mysqli_resource;
1102 : MY_MYSQL *mysql;
1103 :
1104 11 : if (getThis() && ((mysqli_object *) zend_object_store_get_object(getThis() TSRMLS_CC))->ptr) {
1105 1 : return;
1106 : }
1107 :
1108 10 : mysql = (MY_MYSQL *)ecalloc(1, sizeof(MY_MYSQL));
1109 :
1110 10 : if (!(mysql->mysql = mysql_init(NULL))) {
1111 0 : efree(mysql);
1112 0 : RETURN_FALSE;
1113 : }
1114 :
1115 10 : mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
1116 10 : mysqli_resource->ptr = (void *)mysql;
1117 10 : mysqli_resource->status = MYSQLI_STATUS_INITIALIZED;
1118 :
1119 18 : if (!getThis() || !instanceof_function(Z_OBJCE_P(getThis()), mysqli_link_class_entry TSRMLS_CC)) {
1120 8 : MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_link_class_entry);
1121 : } else {
1122 2 : ((mysqli_object *) zend_object_store_get_object(getThis() TSRMLS_CC))->ptr = mysqli_resource;
1123 : }
1124 : }
1125 : /* }}} */
1126 :
1127 : /* {{{ proto mixed mysqli_insert_id(object link)
1128 : Get the ID generated from the previous INSERT operation */
1129 : PHP_FUNCTION(mysqli_insert_id)
1130 2 : {
1131 : MY_MYSQL *mysql;
1132 : my_ulonglong rc;
1133 : zval *mysql_link;
1134 :
1135 2 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1136 0 : return;
1137 : }
1138 2 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1139 2 : rc = mysql_insert_id(mysql->mysql);
1140 2 : MYSQLI_RETURN_LONG_LONG(rc)
1141 : }
1142 : /* }}} */
1143 :
1144 : /* {{{ proto bool mysqli_kill(object link, int processid)
1145 : Kill a mysql process on the server */
1146 : PHP_FUNCTION(mysqli_kill)
1147 2 : {
1148 : MY_MYSQL *mysql;
1149 : zval *mysql_link;
1150 : long processid;
1151 :
1152 2 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &mysql_link, mysqli_link_class_entry, &processid) == FAILURE) {
1153 0 : return;
1154 : }
1155 2 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1156 :
1157 2 : if (mysql_kill(mysql->mysql, processid)) {
1158 0 : MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
1159 0 : RETURN_FALSE;
1160 : }
1161 2 : RETURN_TRUE;
1162 : }
1163 : /* }}} */
1164 :
1165 : /* {{{ proto void mysqli_set_local_infile_default(object link)
1166 : unsets user defined handler for load local infile command */
1167 : PHP_FUNCTION(mysqli_set_local_infile_default)
1168 0 : {
1169 : MY_MYSQL *mysql;
1170 : zval *mysql_link;
1171 :
1172 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1173 0 : return;
1174 : }
1175 :
1176 0 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1177 :
1178 0 : if (mysql->li_read) {
1179 0 : zval_ptr_dtor(&(mysql->li_read));
1180 0 : mysql->li_read = NULL;
1181 : }
1182 : }
1183 : /* }}} */
1184 :
1185 : /* {{{ proto bool mysqli_set_local_infile_handler(object link, callback read_func)
1186 : Set callback functions for LOAD DATA LOCAL INFILE */
1187 : PHP_FUNCTION(mysqli_set_local_infile_handler)
1188 1 : {
1189 : MY_MYSQL *mysql;
1190 : zval *mysql_link;
1191 : char *callback_name;
1192 : zval *callback_func;
1193 :
1194 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz", &mysql_link, mysqli_link_class_entry,
1195 : &callback_func) == FAILURE) {
1196 0 : return;
1197 : }
1198 :
1199 1 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1200 :
1201 : /* check callback function */
1202 1 : if (!zend_is_callable(callback_func, 0, &callback_name)) {
1203 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a valid callback function %s", callback_name);
1204 0 : efree(callback_name);
1205 0 : RETURN_FALSE;
1206 : }
1207 :
1208 : /* save callback function */
1209 1 : if (!mysql->li_read) {
1210 1 : MAKE_STD_ZVAL(mysql->li_read);
1211 : } else {
1212 0 : zval_dtor(mysql->li_read);
1213 : }
1214 1 : ZVAL_STRING(mysql->li_read, callback_name, 0);
1215 :
1216 1 : RETURN_TRUE;
1217 : }
1218 : /* }}} */
1219 :
1220 : /* {{{ proto bool mysqli_more_results(object link)
1221 : check if there any more query results from a multi query */
1222 : PHP_FUNCTION(mysqli_more_results)
1223 5 : {
1224 : MY_MYSQL *mysql;
1225 : zval *mysql_link;
1226 :
1227 5 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1228 0 : return;
1229 : }
1230 5 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1231 :
1232 5 : RETURN_BOOL(mysql_more_results(mysql->mysql));
1233 : }
1234 : /* }}} */
1235 :
1236 : /* {{{ proto bool mysqli_next_result(object link)
1237 : read next result from multi_query */
1238 6 : PHP_FUNCTION(mysqli_next_result) {
1239 : MY_MYSQL *mysql;
1240 : zval *mysql_link;
1241 :
1242 6 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1243 0 : return;
1244 : }
1245 6 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1246 :
1247 6 : RETURN_BOOL(!mysql_next_result(mysql->mysql));
1248 : }
1249 : /* }}} */
1250 :
1251 : /* {{{ proto int mysqli_num_fields(object result)
1252 : Get number of fields in result */
1253 : PHP_FUNCTION(mysqli_num_fields)
1254 2 : {
1255 : MYSQL_RES *result;
1256 : zval *mysql_result;
1257 :
1258 2 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
1259 0 : return;
1260 : }
1261 2 : MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
1262 :
1263 2 : RETURN_LONG(mysql_num_fields(result));
1264 : }
1265 : /* }}} */
1266 :
1267 : /* {{{ proto mixed mysqli_num_rows(object result)
1268 : Get number of rows in result */
1269 : PHP_FUNCTION(mysqli_num_rows)
1270 2 : {
1271 : MYSQL_RES *result;
1272 : zval *mysql_result;
1273 :
1274 2 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
1275 0 : return;
1276 : }
1277 2 : MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
1278 :
1279 2 : if (result->handle && result->handle->status == MYSQL_STATUS_USE_RESULT) {
1280 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Function cannot be used with MYSQL_USE_RESULT");
1281 0 : RETURN_LONG(0);
1282 : }
1283 :
1284 2 : MYSQLI_RETURN_LONG_LONG(mysql_num_rows(result));
1285 : }
1286 : /* }}} */
1287 :
1288 : /* {{{ proto bool mysqli_options(object link, int flags, mixed values)
1289 : Set options */
1290 : PHP_FUNCTION(mysqli_options)
1291 1 : {
1292 : MY_MYSQL *mysql;
1293 1 : zval *mysql_link = NULL;
1294 : zval *mysql_value;
1295 : long mysql_option;
1296 : unsigned int l_value;
1297 : long ret;
1298 :
1299 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Olz", &mysql_link, mysqli_link_class_entry, &mysql_option, &mysql_value) == FAILURE) {
1300 0 : return;
1301 : }
1302 1 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_INITIALIZED);
1303 :
1304 1 : if ((PG(open_basedir) && PG(open_basedir)[0] != '\0') || PG(safe_mode)) {
1305 0 : if(mysql_option == MYSQL_OPT_LOCAL_INFILE) {
1306 0 : RETURN_FALSE;
1307 : }
1308 : }
1309 :
1310 1 : switch (Z_TYPE_PP(&mysql_value)) {
1311 : case IS_STRING:
1312 0 : ret = mysql_options(mysql->mysql, mysql_option, Z_STRVAL_PP(&mysql_value));
1313 0 : break;
1314 : default:
1315 1 : convert_to_long_ex(&mysql_value);
1316 1 : l_value = Z_LVAL_PP(&mysql_value);
1317 1 : ret = mysql_options(mysql->mysql, mysql_option, (char *)&l_value);
1318 : break;
1319 : }
1320 :
1321 1 : RETURN_BOOL(!ret);
1322 : }
1323 : /* }}} */
1324 :
1325 :
1326 : /* {{{ proto bool mysqli_ping(object link)
1327 : Ping a server connection or reconnect if there is no connection */
1328 : PHP_FUNCTION(mysqli_ping)
1329 4 : {
1330 : MY_MYSQL *mysql;
1331 : zval *mysql_link;
1332 : long rc;
1333 :
1334 4 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1335 0 : return;
1336 : }
1337 4 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1338 4 : rc = mysql_ping(mysql->mysql);
1339 4 : MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
1340 :
1341 4 : RETURN_BOOL(!rc);
1342 : }
1343 : /* }}} */
1344 :
1345 : /* {{{ proto mixed mysqli_prepare(object link, string query)
1346 : Prepare a SQL statement for execution */
1347 : PHP_FUNCTION(mysqli_prepare)
1348 59 : {
1349 : MY_MYSQL *mysql;
1350 : MY_STMT *stmt;
1351 59 : char *query = NULL;
1352 : unsigned int query_len;
1353 : zval *mysql_link;
1354 : MYSQLI_RESOURCE *mysqli_resource;
1355 :
1356 59 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",&mysql_link, mysqli_link_class_entry, &query, &query_len) == FAILURE) {
1357 0 : return;
1358 : }
1359 59 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1360 59 : if (mysql->mysql->status == MYSQL_STATUS_GET_RESULT) {
1361 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "All data must be fetched before a new statement prepare takes place");
1362 0 : RETURN_FALSE;
1363 : }
1364 :
1365 59 : stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT));
1366 :
1367 59 : if ((stmt->stmt = mysql_stmt_init(mysql->mysql))) {
1368 59 : if (mysql_stmt_prepare(stmt->stmt, query, query_len)) {
1369 : char last_error[MYSQL_ERRMSG_SIZE];
1370 : char sqlstate[SQLSTATE_LENGTH+1];
1371 : unsigned int last_errno;
1372 :
1373 : /* mysql_stmt_close clears errors, so we have to store them temporarily */
1374 1 : last_errno = stmt->stmt->last_errno;
1375 1 : memcpy(last_error, stmt->stmt->last_error, MYSQL_ERRMSG_SIZE);
1376 1 : memcpy(sqlstate, mysql->mysql->net.sqlstate, SQLSTATE_LENGTH+1);
1377 :
1378 1 : mysql_stmt_close(stmt->stmt);
1379 1 : stmt->stmt = NULL;
1380 :
1381 : /* restore error messages */
1382 1 : mysql->mysql->net.last_errno = last_errno;
1383 1 : memcpy(mysql->mysql->net.last_error, last_error, MYSQL_ERRMSG_SIZE);
1384 1 : memcpy(mysql->mysql->net.sqlstate, sqlstate, SQLSTATE_LENGTH+1);
1385 : }
1386 : }
1387 : /* don't joing to the previous if because it won't work if mysql_stmt_prepare_fails */
1388 59 : if (!stmt->stmt) {
1389 1 : MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
1390 1 : efree(stmt);
1391 1 : RETURN_FALSE;
1392 : }
1393 :
1394 :
1395 58 : mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
1396 58 : mysqli_resource->ptr = (void *)stmt;
1397 : /* don't initialize stmt->query with NULL, we ecalloc()-ed the memory */
1398 : /* Get performance boost if reporting is switched off */
1399 58 : if (query_len && (MyG(report_mode) & MYSQLI_REPORT_INDEX)) {
1400 0 : stmt->query = (char *)emalloc(query_len + 1);
1401 0 : memcpy(stmt->query, query, query_len);
1402 0 : stmt->query[query_len] = '\0';
1403 : }
1404 :
1405 : /* change status */
1406 58 : mysqli_resource->status = MYSQLI_STATUS_VALID;
1407 58 : MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_stmt_class_entry);
1408 : }
1409 : /* }}} */
1410 :
1411 : /* {{{ proto bool mysqli_real_connect(object link [,string hostname [,string username [,string passwd [,string dbname [,int port [,string socket [,int flags]]]]]]])
1412 : Open a connection to a mysql server */
1413 : PHP_FUNCTION(mysqli_real_connect)
1414 7 : {
1415 : MY_MYSQL *mysql;
1416 7 : char *hostname = NULL, *username=NULL, *passwd=NULL, *dbname=NULL, *socket=NULL;
1417 7 : unsigned int hostname_len = 0, username_len = 0, passwd_len = 0, dbname_len = 0, socket_len = 0;
1418 7 : unsigned long port=0, flags=0;
1419 : zval *mysql_link;
1420 :
1421 7 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|sssslsl", &mysql_link, mysqli_link_class_entry,
1422 : &hostname, &hostname_len, &username, &username_len, &passwd, &passwd_len, &dbname, &dbname_len, &port, &socket, &socket_len,
1423 : &flags) == FAILURE) {
1424 0 : return;
1425 : }
1426 :
1427 7 : if (!socket_len) {
1428 7 : socket = NULL;
1429 : }
1430 :
1431 : /* TODO: safe mode handling */
1432 7 : if (PG(sql_safe_mode)) {
1433 : } else {
1434 7 : if (!socket_len || !socket) {
1435 7 : socket = MyG(default_socket);
1436 : }
1437 7 : if (!port) {
1438 6 : port = MyG(default_port);
1439 : }
1440 7 : if (!passwd) {
1441 0 : passwd = MyG(default_pw);
1442 : }
1443 7 : if (!username){
1444 0 : username = MyG(default_user);
1445 : }
1446 7 : if (!hostname) {
1447 0 : hostname = MyG(default_host);
1448 : }
1449 : }
1450 :
1451 7 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_INITIALIZED);
1452 :
1453 :
1454 : /* set some required options */
1455 7 : flags |= CLIENT_MULTI_RESULTS; /* needed for mysql_multi_query() */
1456 : /* remove some insecure options */
1457 7 : flags &= ~CLIENT_MULTI_STATEMENTS; /* don't allow multi_queries via connect parameter */
1458 7 : if ((PG(open_basedir) && PG(open_basedir)[0] != '\0') || PG(safe_mode)) {
1459 0 : flags &= ~CLIENT_LOCAL_FILES;
1460 : }
1461 :
1462 7 : if (!socket) {
1463 7 : socket = MyG(default_socket);
1464 : }
1465 :
1466 7 : if (mysql_real_connect(mysql->mysql,hostname,username,passwd,dbname,port,socket,flags) == NULL) {
1467 0 : php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql) TSRMLS_CC);
1468 0 : php_mysqli_throw_sql_exception( mysql->mysql->net.sqlstate, mysql->mysql->net.last_errno TSRMLS_CC,
1469 : "%s", mysql->mysql->net.last_error);
1470 :
1471 : /* change status */
1472 0 : MYSQLI_SET_STATUS(&mysql_link, MYSQLI_STATUS_INITIALIZED);
1473 0 : RETURN_FALSE;
1474 : }
1475 :
1476 7 : php_mysqli_set_error(mysql_errno(mysql->mysql), (char *)mysql_error(mysql->mysql) TSRMLS_CC);
1477 :
1478 7 : mysql->mysql->reconnect = MyG(reconnect);
1479 :
1480 : /* set our own local_infile handler */
1481 7 : php_set_local_infile_handler_default(mysql);
1482 :
1483 : /* change status */
1484 7 : MYSQLI_SET_STATUS(&mysql_link, MYSQLI_STATUS_VALID);
1485 :
1486 7 : RETURN_TRUE;
1487 : }
1488 : /* }}} */
1489 :
1490 : /* {{{ proto bool mysqli_real_query(object link, string query)
1491 : Binary-safe version of mysql_query() */
1492 : PHP_FUNCTION(mysqli_real_query)
1493 6 : {
1494 : MY_MYSQL *mysql;
1495 : zval *mysql_link;
1496 6 : char *query = NULL;
1497 : unsigned int query_len;
1498 :
1499 6 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &mysql_link, mysqli_link_class_entry, &query, &query_len) == FAILURE) {
1500 0 : return;
1501 : }
1502 6 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1503 :
1504 6 : MYSQLI_DISABLE_MQ; /* disable multi statements/queries */
1505 :
1506 6 : if (mysql_real_query(mysql->mysql, query, query_len)) {
1507 0 : MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
1508 0 : RETURN_FALSE;
1509 : }
1510 :
1511 6 : if (!mysql_field_count(mysql->mysql)) {
1512 0 : if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
1513 0 : php_mysqli_report_index(query, mysql->mysql->server_status TSRMLS_CC);
1514 : }
1515 : }
1516 :
1517 6 : RETURN_TRUE;
1518 : }
1519 : /* }}} */
1520 :
1521 : /* {{{ proto string mysqli_real_escape_string(object link, string escapestr)
1522 : Escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection */
1523 0 : PHP_FUNCTION(mysqli_real_escape_string) {
1524 : MY_MYSQL *mysql;
1525 0 : zval *mysql_link = NULL;
1526 : char *escapestr, *newstr;
1527 : int escapestr_len, newstr_len;
1528 :
1529 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &mysql_link, mysqli_link_class_entry, &escapestr, &escapestr_len) == FAILURE) {
1530 0 : return;
1531 : }
1532 0 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1533 :
1534 0 : newstr = safe_emalloc(2, escapestr_len, 1);
1535 0 : newstr_len = mysql_real_escape_string(mysql->mysql, newstr, escapestr, escapestr_len);
1536 0 : newstr = erealloc(newstr, newstr_len + 1);
1537 :
1538 0 : RETURN_STRINGL(newstr, newstr_len, 0);
1539 : }
1540 : /* }}} */
1541 :
1542 : /* {{{ proto bool mysqli_rollback(object link)
1543 : Undo actions from current transaction */
1544 : PHP_FUNCTION(mysqli_rollback)
1545 2 : {
1546 : MY_MYSQL *mysql;
1547 : zval *mysql_link;
1548 :
1549 2 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1550 0 : return;
1551 : }
1552 2 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1553 :
1554 2 : if (mysql_rollback(mysql->mysql)) {
1555 0 : RETURN_FALSE;
1556 : }
1557 2 : RETURN_TRUE;
1558 : }
1559 : /* }}} */
1560 :
1561 : /* {{{ proto bool mysqli_stmt_send_long_data(object stmt, int param_nr, string data)
1562 : */
1563 : PHP_FUNCTION(mysqli_stmt_send_long_data)
1564 3 : {
1565 : MY_STMT *stmt;
1566 : zval *mysql_stmt;
1567 : char *data;
1568 : long param_nr;
1569 : int data_len;
1570 :
1571 :
1572 3 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ols", &mysql_stmt, mysqli_stmt_class_entry, ¶m_nr, &data, &data_len) == FAILURE) {
1573 0 : return;
1574 : }
1575 3 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1576 :
1577 3 : if (param_nr < 0) {
1578 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parameter number");
1579 0 : RETURN_FALSE;
1580 : }
1581 3 : if (mysql_stmt_send_long_data(stmt->stmt, param_nr, data, data_len)) {
1582 0 : RETURN_FALSE;
1583 : }
1584 3 : RETURN_TRUE;
1585 : }
1586 : /* }}} */
1587 :
1588 :
1589 : /* {{{ proto mixed mysqli_stmt_affected_rows(object stmt)
1590 : Return the number of rows affected in the last query for the given link */
1591 : PHP_FUNCTION(mysqli_stmt_affected_rows)
1592 2 : {
1593 : MY_STMT *stmt;
1594 : zval *mysql_stmt;
1595 : my_ulonglong rc;
1596 :
1597 2 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1598 0 : return;
1599 : }
1600 2 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1601 :
1602 2 : rc = mysql_stmt_affected_rows(stmt->stmt);
1603 2 : if (rc == (my_ulonglong) -1) {
1604 0 : RETURN_LONG(-1);
1605 : }
1606 2 : MYSQLI_RETURN_LONG_LONG(rc)
1607 : }
1608 : /* }}} */
1609 :
1610 : /* {{{ proto bool mysqli_stmt_close(object stmt)
1611 : Close statement */
1612 : PHP_FUNCTION(mysqli_stmt_close)
1613 56 : {
1614 : MY_STMT *stmt;
1615 : zval *mysql_stmt;
1616 :
1617 56 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1618 0 : return;
1619 : }
1620 56 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1621 :
1622 56 : mysql_stmt_close(stmt->stmt);
1623 56 : stmt->stmt = NULL;
1624 56 : php_clear_stmt_bind(stmt);
1625 56 : MYSQLI_CLEAR_RESOURCE(&mysql_stmt);
1626 56 : RETURN_TRUE;
1627 : }
1628 : /* }}} */
1629 :
1630 : /* {{{ proto void mysqli_stmt_data_seek(object stmt, int offset)
1631 : Move internal result pointer */
1632 : PHP_FUNCTION(mysqli_stmt_data_seek)
1633 0 : {
1634 : MY_STMT *stmt;
1635 : zval *mysql_stmt;
1636 : long offset;
1637 :
1638 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &mysql_stmt, mysqli_stmt_class_entry, &offset) == FAILURE) {
1639 0 : return;
1640 : }
1641 0 : if (offset < 0) {
1642 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset must be positive");
1643 0 : RETURN_FALSE;
1644 : }
1645 :
1646 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1647 :
1648 0 : mysql_stmt_data_seek(stmt->stmt, offset);
1649 : }
1650 : /* }}} */
1651 :
1652 : /* {{{ proto int mysqli_stmt_field_count(object stmt) {
1653 : Return the number of result columns for the given statement */
1654 : PHP_FUNCTION(mysqli_stmt_field_count)
1655 0 : {
1656 : MY_STMT *stmt;
1657 : zval *mysql_stmt;
1658 :
1659 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1660 0 : return;
1661 : }
1662 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1663 :
1664 0 : RETURN_LONG(mysql_stmt_field_count(stmt->stmt));
1665 : }
1666 : /* }}} */
1667 :
1668 : /* {{{ proto void mysqli_stmt_free_result(object stmt)
1669 : Free stored result memory for the given statement handle */
1670 : PHP_FUNCTION(mysqli_stmt_free_result)
1671 0 : {
1672 : MY_STMT *stmt;
1673 : zval *mysql_stmt;
1674 :
1675 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1676 0 : return;
1677 : }
1678 :
1679 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1680 :
1681 0 : mysql_stmt_free_result(stmt->stmt);
1682 : }
1683 : /* }}} */
1684 :
1685 : /* {{{ proto mixed mysqli_stmt_insert_id(object stmt)
1686 : Get the ID generated from the previous INSERT operation */
1687 : PHP_FUNCTION(mysqli_stmt_insert_id)
1688 0 : {
1689 : MY_STMT *stmt;
1690 : my_ulonglong rc;
1691 : zval *mysql_stmt;
1692 :
1693 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1694 0 : return;
1695 : }
1696 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1697 0 : rc = mysql_stmt_insert_id(stmt->stmt);
1698 0 : MYSQLI_RETURN_LONG_LONG(rc)
1699 : }
1700 : /* }}} */
1701 :
1702 : /* {{{ proto int mysqli_stmt_param_count(object stmt) {
1703 : Return the number of parameter for the given statement */
1704 : PHP_FUNCTION(mysqli_stmt_param_count)
1705 0 : {
1706 : MY_STMT *stmt;
1707 : zval *mysql_stmt;
1708 :
1709 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1710 0 : return;
1711 : }
1712 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1713 :
1714 0 : RETURN_LONG(mysql_stmt_param_count(stmt->stmt));
1715 : }
1716 : /* }}} */
1717 :
1718 : /* {{{ proto bool mysqli_stmt_reset(object stmt)
1719 : reset a prepared statement */
1720 : PHP_FUNCTION(mysqli_stmt_reset)
1721 0 : {
1722 : MY_STMT *stmt;
1723 : zval *mysql_stmt;
1724 :
1725 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1726 0 : return;
1727 : }
1728 :
1729 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1730 :
1731 0 : if (mysql_stmt_reset(stmt->stmt)) {
1732 0 : RETURN_FALSE;
1733 : }
1734 0 : RETURN_TRUE;
1735 : }
1736 : /* }}} */
1737 :
1738 : /* {{{ proto mixed mysqli_stmt_num_rows(object stmt)
1739 : Return the number of rows in statements result set */
1740 : PHP_FUNCTION(mysqli_stmt_num_rows)
1741 0 : {
1742 : MY_STMT *stmt;
1743 : zval *mysql_stmt;
1744 : my_ulonglong rc;
1745 :
1746 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1747 0 : return;
1748 : }
1749 :
1750 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1751 :
1752 0 : rc = mysql_stmt_num_rows(stmt->stmt);
1753 0 : MYSQLI_RETURN_LONG_LONG(rc)
1754 : }
1755 : /* }}} */
1756 :
1757 : /* {{{ proto string mysqli_select_db(object link, string dbname)
1758 : Select a MySQL database */
1759 : PHP_FUNCTION(mysqli_select_db)
1760 46 : {
1761 : MY_MYSQL *mysql;
1762 : zval *mysql_link;
1763 : char *dbname;
1764 : int dbname_len;
1765 :
1766 :
1767 46 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &mysql_link, mysqli_link_class_entry, &dbname, &dbname_len) == FAILURE) {
1768 0 : return;
1769 : }
1770 46 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1771 :
1772 46 : if (!mysql_select_db(mysql->mysql, dbname)) {
1773 45 : RETURN_TRUE;
1774 : }
1775 :
1776 1 : MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
1777 1 : RETURN_FALSE;
1778 : }
1779 : /* }}} */
1780 :
1781 : /* {{{ proto string mysqli_sqlstate(object link)
1782 : Returns the SQLSTATE error from previous MySQL operation */
1783 : PHP_FUNCTION(mysqli_sqlstate)
1784 0 : {
1785 : MY_MYSQL *mysql;
1786 : zval *mysql_link;
1787 :
1788 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1789 0 : return;
1790 : }
1791 0 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1792 0 : RETURN_STRING((char *)mysql_sqlstate(mysql->mysql),1);
1793 : }
1794 : /* }}} */
1795 :
1796 : /* {{{ proto bool mysqli_ssl_set(object link ,string key ,string cert ,string ca ,string capath ,string cipher])
1797 : */
1798 : PHP_FUNCTION(mysqli_ssl_set)
1799 0 : {
1800 : MY_MYSQL *mysql;
1801 : zval *mysql_link;
1802 : char *ssl_parm[5];
1803 : int ssl_parm_len[5], i;
1804 :
1805 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osssss", &mysql_link, mysqli_link_class_entry, &ssl_parm[0], &ssl_parm_len[0], &ssl_parm[1], &ssl_parm_len[1], &ssl_parm[2], &ssl_parm_len[2], &ssl_parm[3], &ssl_parm_len[3], &ssl_parm[4], &ssl_parm_len[4]) == FAILURE) {
1806 0 : return;
1807 : }
1808 0 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_INITIALIZED);
1809 :
1810 0 : for (i=0; i < 5; i++) {
1811 0 : if (!ssl_parm_len[i]) {
1812 0 : ssl_parm[i] = NULL;
1813 : }
1814 : }
1815 :
1816 0 : mysql_ssl_set(mysql->mysql, ssl_parm[0], ssl_parm[1], ssl_parm[2], ssl_parm[3], ssl_parm[4]);
1817 :
1818 0 : RETURN_TRUE;
1819 : }
1820 : /* }}} */
1821 :
1822 : /* {{{ proto mixed mysqli_stat(object link)
1823 : Get current system status */
1824 : PHP_FUNCTION(mysqli_stat)
1825 1 : {
1826 : MY_MYSQL *mysql;
1827 : zval *mysql_link;
1828 : char *stat;
1829 :
1830 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1831 0 : return;
1832 : }
1833 1 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1834 :
1835 1 : if ((stat = (char *)mysql_stat(mysql->mysql))) {
1836 1 : RETURN_STRING(stat, 1);
1837 : }
1838 0 : RETURN_FALSE;
1839 : }
1840 :
1841 : /* }}} */
1842 :
1843 : /* {{{ proto int mysqli_stmt_attr_set(object stmt, long attr, long mode)
1844 : */
1845 : PHP_FUNCTION(mysqli_stmt_attr_set)
1846 3 : {
1847 : MY_STMT *stmt;
1848 : zval *mysql_stmt;
1849 : ulong mode;
1850 : ulong attr;
1851 :
1852 3 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oll", &mysql_stmt, mysqli_stmt_class_entry, &attr, &mode) == FAILURE) {
1853 0 : return;
1854 : }
1855 3 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1856 :
1857 3 : if (!mysql_stmt_attr_set(stmt->stmt, attr, (void *)&mode)) {
1858 3 : RETURN_FALSE;
1859 : }
1860 0 : RETURN_TRUE;
1861 : }
1862 : /* }}} */
1863 :
1864 : /* {{{ proto int mysqli_stmt_attr_get(object stmt, long attr)
1865 : */
1866 : PHP_FUNCTION(mysqli_stmt_attr_get)
1867 0 : {
1868 : MY_STMT *stmt;
1869 : zval *mysql_stmt;
1870 : #if MYSQL_VERSION_ID > 50099
1871 : my_bool value;
1872 : #else
1873 0 : ulong value = 0;
1874 : #endif
1875 : ulong attr;
1876 : int rc;
1877 :
1878 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &mysql_stmt, mysqli_stmt_class_entry, &attr) == FAILURE) {
1879 0 : return;
1880 : }
1881 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1882 :
1883 0 : if ((rc = mysql_stmt_attr_get(stmt->stmt, attr, &value))) {
1884 0 : RETURN_FALSE;
1885 : }
1886 0 : RETURN_LONG((long)value);
1887 : }
1888 : /* }}} */
1889 :
1890 : /* {{{ proto int mysqli_stmt_errno(object stmt)
1891 : */
1892 : PHP_FUNCTION(mysqli_stmt_errno)
1893 0 : {
1894 : MY_STMT *stmt;
1895 : zval *mysql_stmt;
1896 :
1897 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1898 0 : return;
1899 : }
1900 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_INITIALIZED);
1901 :
1902 0 : RETURN_LONG(mysql_stmt_errno(stmt->stmt));
1903 : }
1904 : /* }}} */
1905 :
1906 : /* {{{ proto string mysqli_stmt_error(object stmt)
1907 : */
1908 : PHP_FUNCTION(mysqli_stmt_error)
1909 0 : {
1910 : MY_STMT *stmt;
1911 : zval *mysql_stmt;
1912 :
1913 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1914 0 : return;
1915 : }
1916 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_INITIALIZED);
1917 :
1918 0 : RETURN_STRING((char *)mysql_stmt_error(stmt->stmt),1);
1919 : }
1920 : /* }}} */
1921 :
1922 : /* {{{ proto mixed mysqli_stmt_init(object link)
1923 : Initialize statement object
1924 : */
1925 : PHP_FUNCTION(mysqli_stmt_init)
1926 1 : {
1927 : MY_MYSQL *mysql;
1928 : MY_STMT *stmt;
1929 : zval *mysql_link;
1930 : MYSQLI_RESOURCE *mysqli_resource;
1931 :
1932 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",&mysql_link, mysqli_link_class_entry) == FAILURE) {
1933 0 : return;
1934 : }
1935 1 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
1936 :
1937 1 : stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT));
1938 :
1939 1 : if (!(stmt->stmt = mysql_stmt_init(mysql->mysql))) {
1940 0 : efree(stmt);
1941 0 : RETURN_FALSE;
1942 : }
1943 :
1944 1 : mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
1945 1 : mysqli_resource->status = MYSQLI_STATUS_INITIALIZED;
1946 1 : mysqli_resource->ptr = (void *)stmt;
1947 1 : MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_stmt_class_entry);
1948 : }
1949 : /* }}} */
1950 :
1951 : /* {{{ proto bool mysqli_stmt_prepare(object stmt, string query)
1952 : prepare server side statement with query
1953 : */
1954 : PHP_FUNCTION(mysqli_stmt_prepare)
1955 1 : {
1956 : MY_STMT *stmt;
1957 : zval *mysql_stmt;
1958 : char *query;
1959 : int query_len;
1960 :
1961 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &mysql_stmt, mysqli_stmt_class_entry, &query, &query_len) == FAILURE) {
1962 0 : return;
1963 : }
1964 1 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_INITIALIZED);
1965 :
1966 1 : if (mysql_stmt_prepare(stmt->stmt, query, query_len)) {
1967 0 : MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
1968 0 : RETURN_FALSE;
1969 : }
1970 : /* change status */
1971 1 : MYSQLI_SET_STATUS(&mysql_stmt, MYSQLI_STATUS_VALID);
1972 1 : RETURN_TRUE;
1973 : }
1974 : /* }}} */
1975 :
1976 : /* {{{ proto mixed mysqli_stmt_result_metadata(object stmt)
1977 : return result set from statement */
1978 : PHP_FUNCTION(mysqli_stmt_result_metadata)
1979 2 : {
1980 : MY_STMT *stmt;
1981 : MYSQL_RES *result;
1982 : zval *mysql_stmt;
1983 : MYSQLI_RESOURCE *mysqli_resource;
1984 :
1985 2 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1986 0 : return;
1987 : }
1988 2 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
1989 :
1990 2 : if (!(result = mysql_stmt_result_metadata(stmt->stmt))){
1991 0 : MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
1992 0 : RETURN_FALSE;
1993 : }
1994 :
1995 2 : mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
1996 2 : mysqli_resource->ptr = (void *)result;
1997 2 : mysqli_resource->status = MYSQLI_STATUS_VALID;
1998 2 : MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
1999 : }
2000 : /* }}} */
2001 :
2002 : /* {{{ proto bool mysqli_stmt_store_result(stmt)
2003 : */
2004 : PHP_FUNCTION(mysqli_stmt_store_result)
2005 2 : {
2006 : MY_STMT *stmt;
2007 : zval *mysql_stmt;
2008 2 : int i=0;
2009 :
2010 2 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
2011 0 : return;
2012 : }
2013 2 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
2014 :
2015 : /*
2016 : If the user wants to store the data and we have BLOBs/TEXTs we try to allocate
2017 : not the maximal length of the type (which is 16MB even for LONGBLOB) but
2018 : the maximal length of the field in the result set. If he/she has quite big
2019 : BLOB/TEXT columns after calling store_result() the memory usage of PHP will
2020 : double - but this is a known problem of the simple MySQL API ;)
2021 : */
2022 3 : for (i = mysql_stmt_field_count(stmt->stmt) - 1; i >=0; --i) {
2023 2 : if (stmt->stmt->fields && (stmt->stmt->fields[i].type == MYSQL_TYPE_BLOB ||
2024 : stmt->stmt->fields[i].type == MYSQL_TYPE_MEDIUM_BLOB ||
2025 : stmt->stmt->fields[i].type == MYSQL_TYPE_LONG_BLOB))
2026 : {
2027 1 : my_bool tmp=1;
2028 1 : mysql_stmt_attr_set(stmt->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &tmp);
2029 1 : break;
2030 : }
2031 : }
2032 :
2033 2 : if (mysql_stmt_store_result(stmt->stmt)){
2034 0 : MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
2035 0 : RETURN_FALSE;
2036 : }
2037 2 : RETURN_TRUE;
2038 : }
2039 : /* }}} */
2040 :
2041 : /* {{{ proto string mysqli_stmt_sqlstate(object stmt)
2042 : */
2043 : PHP_FUNCTION(mysqli_stmt_sqlstate)
2044 0 : {
2045 : MY_STMT *stmt;
2046 : zval *mysql_stmt;
2047 :
2048 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
2049 0 : return;
2050 : }
2051 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
2052 :
2053 0 : RETURN_STRING((char *)mysql_stmt_sqlstate(stmt->stmt),1);
2054 : }
2055 : /* }}} */
2056 :
2057 : /* {{{ proto object mysqli_store_result(object link)
2058 : Buffer result set on client */
2059 : PHP_FUNCTION(mysqli_store_result)
2060 10 : {
2061 : MY_MYSQL *mysql;
2062 : MYSQL_RES *result;
2063 : zval *mysql_link;
2064 : MYSQLI_RESOURCE *mysqli_resource;
2065 :
2066 10 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
2067 0 : return;
2068 : }
2069 10 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
2070 :
2071 10 : if (!(result = mysql_store_result(mysql->mysql))) {
2072 1 : MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
2073 1 : RETURN_FALSE;
2074 : }
2075 9 : if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
2076 0 : php_mysqli_report_index("from previous query", mysql->mysql->server_status TSRMLS_CC);
2077 : }
2078 :
2079 9 : mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
2080 9 : mysqli_resource->ptr = (void *)result;
2081 9 : mysqli_resource->status = MYSQLI_STATUS_VALID;
2082 9 : MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
2083 : }
2084 : /* }}} */
2085 :
2086 : /* {{{ proto int mysqli_thread_id(object link)
2087 : Return the current thread ID */
2088 : PHP_FUNCTION(mysqli_thread_id)
2089 1 : {
2090 : MY_MYSQL *mysql;
2091 : zval *mysql_link;
2092 :
2093 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
2094 0 : return;
2095 : }
2096 1 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
2097 :
2098 1 : RETURN_LONG(mysql_thread_id(mysql->mysql));
2099 : }
2100 : /* }}} */
2101 :
2102 : /* {{{ proto bool mysqli_thread_safe(void)
2103 : Return whether thread safety is given or not */
2104 : PHP_FUNCTION(mysqli_thread_safe)
2105 0 : {
2106 0 : RETURN_BOOL(mysql_thread_safe());
2107 : }
2108 :
2109 : /* }}} */
2110 :
2111 : /* {{{ proto mixed mysqli_use_result(object link)
2112 : Directly retrieve query results - do not buffer results on client side */
2113 : PHP_FUNCTION(mysqli_use_result)
2114 0 : {
2115 : MY_MYSQL *mysql;
2116 : MYSQL_RES *result;
2117 : zval *mysql_link;
2118 : MYSQLI_RESOURCE *mysqli_resource;
2119 :
2120 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
2121 0 : return;
2122 : }
2123 0 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
2124 :
2125 0 : if (!(result = mysql_use_result(mysql->mysql))) {
2126 0 : MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
2127 0 : RETURN_FALSE;
2128 : }
2129 :
2130 0 : if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
2131 0 : php_mysqli_report_index("from previous query", mysql->mysql->server_status TSRMLS_CC);
2132 : }
2133 0 : mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
2134 0 : mysqli_resource->ptr = (void *)result;
2135 0 : mysqli_resource->status = MYSQLI_STATUS_VALID;
2136 0 : MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
2137 : }
2138 : /* }}} */
2139 :
2140 : /* {{{ proto int mysqli_warning_count (object link)
2141 : Return number of warnings from the last query for the given link */
2142 : PHP_FUNCTION(mysqli_warning_count)
2143 1 : {
2144 : MY_MYSQL *mysql;
2145 : zval *mysql_link;
2146 :
2147 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
2148 0 : return;
2149 : }
2150 1 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
2151 :
2152 1 : RETURN_LONG(mysql_warning_count(mysql->mysql));
2153 : }
2154 : /* }}} */
2155 :
2156 : /*
2157 : * Local variables:
2158 : * tab-width: 4
2159 : * c-basic-offset: 4
2160 : * End:
2161 : * vim600: noet sw=4 ts=4 fdm=marker
2162 : * vim<600: noet sw=4 ts=4
2163 : */
|