Skip to content

Commit 6997d50

Browse files
author
Georg Richter
committed
added new function mysqli_stmt_affected_rows
added bigint support for mysqli_affected_rows fixed memleak in mysqli_prepare (stmt->is_null) fixed return type for mysqli_connect
1 parent 6d300ba commit 6997d50

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

ext/mysqli/mysqli_api.c

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,27 @@
3030
#include "php_mysqli.h"
3131

3232

33-
/* {{{ proto long mysqli_affected_rows(resource link)
33+
/* {{{ proto mixed mysqli_affected_rows(resource link)
3434
*/
3535
PHP_FUNCTION(mysqli_affected_rows)
3636
{
37-
MYSQL *mysql;
38-
zval *mysql_link;
37+
MYSQL *mysql;
38+
zval *mysql_link;
39+
my_ulonglong rc;
40+
char ret[40];
3941

4042
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
4143
return;
4244
}
4345

4446
MYSQLI_FETCH_RESOURCE(mysql, MYSQL *, &mysql_link, "mysqli_link");
4547

46-
RETURN_LONG(mysql_affected_rows(mysql));
48+
rc = mysql_affected_rows(mysql);
49+
if (rc != (long)rc) {
50+
sprintf((char *)&ret, "%llu", rc);
51+
RETURN_STRING(ret,1);
52+
}
53+
RETURN_LONG(rc);
4754
}
4855
/* }}} */
4956

@@ -112,7 +119,7 @@ PHP_FUNCTION(mysqli_bind_param)
112119
efree(args);
113120
RETURN_FALSE;
114121
}
115-
122+
stmt->is_null = ecalloc(num_vars, sizeof(char));
116123
bind = (MYSQL_BIND *)ecalloc(num_vars, sizeof(MYSQL_BIND));
117124

118125
for (i=start; i < num_vars * 2 + start; i+=2) {
@@ -988,14 +995,14 @@ PHP_FUNCTION(mysqli_init)
988995
}
989996
/* }}} */
990997

991-
/* {{{ proto int mysqli_insert_id(resource link)
998+
/* {{{ proto mixed mysqli_insert_id(resource link)
992999
*/
9931000
PHP_FUNCTION(mysqli_insert_id)
9941001
{
995-
MYSQL *mysql;
996-
my_ulonglong rc;
997-
char ret[50];
998-
zval *mysql_link;
1002+
MYSQL *mysql;
1003+
my_ulonglong rc;
1004+
char ret[50];
1005+
zval *mysql_link;
9991006

10001007
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
10011008
return;
@@ -1004,11 +1011,10 @@ PHP_FUNCTION(mysqli_insert_id)
10041011
rc = mysql_insert_id(mysql);
10051012

10061013
if (rc != (long)rc) {
1007-
sprintf((char *)&ret, "%lld", rc);
1014+
sprintf((char *)&ret, "%llu", rc);
10081015
RETURN_STRING(ret,1);
1009-
} else {
1010-
RETURN_LONG(rc);
1011-
}
1016+
}
1017+
RETURN_LONG(rc);
10121018
}
10131019
/* }}} */
10141020

@@ -1181,10 +1187,6 @@ PHP_FUNCTION(mysqli_prepare)
11811187
RETURN_FALSE;
11821188
}
11831189

1184-
if (mysql_param_count(stmt->stmt)) {
1185-
stmt->is_null = (char *)emalloc(mysql_param_count(stmt->stmt));
1186-
}
1187-
11881190
MYSQLI_RETURN_RESOURCE(stmt, mysqli_stmt_class_entry);
11891191
}
11901192
/* }}} */
@@ -1495,9 +1497,34 @@ PHP_FUNCTION(mysqli_store_result)
14951497
}
14961498
/* }}} */
14971499

1500+
/* {{{ proto mixed mysqli_stmt_affected_rows(object stmt)
1501+
*/
1502+
PHP_FUNCTION(mysqli_stmt_affected_rows)
1503+
{
1504+
STMT *stmt;
1505+
zval *mysql_stmt;
1506+
my_ulonglong rc;
1507+
char ret[50];
1508+
1509+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1510+
return;
1511+
}
1512+
MYSQLI_FETCH_RESOURCE(stmt, STMT *, &mysql_stmt, "mysqli_stmt");
1513+
1514+
rc = mysql_stmt_affected_rows(stmt->stmt);
1515+
if (rc != (long)rc) {
1516+
sprintf((char *)&ret, "%llu", rc);
1517+
RETURN_STRING(ret, 1);
1518+
}
1519+
1520+
RETURN_LONG(rc);
1521+
}
1522+
/* }}} */
1523+
14981524
/* {{{ proto bool mysqli_stmt_close(resource stmt)
14991525
close statement */
1500-
PHP_FUNCTION(mysqli_stmt_close) {
1526+
PHP_FUNCTION(mysqli_stmt_close)
1527+
{
15011528
STMT *stmt;
15021529
zval *mysql_stmt;
15031530

ext/mysqli/mysqli_fe.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ function_entry mysqli_functions[] = {
105105
PHP_FE(mysqli_slave_query, NULL)
106106
PHP_FE(mysqli_ssl_set, NULL)
107107
PHP_FE(mysqli_stat, NULL)
108+
PHP_FE(mysqli_stmt_affected_rows, NULL)
108109
PHP_FE(mysqli_stmt_close, NULL)
109110
PHP_FE(mysqli_stmt_errno, NULL)
110111
PHP_FE(mysqli_stmt_error, NULL)
@@ -211,6 +212,7 @@ function_entry mysqli_stmt_methods[] = {
211212
PHP_FALIAS(fetch,mysqli_fetch,NULL)
212213
PHP_FALIAS(param_count,mysqli_param_count,NULL)
213214
PHP_FALIAS(send_long_data,mysqli_send_long_data,NULL)
215+
PHP_FALIAS(affected_rows,mysqli_stmt_affected_rows,NULL)
214216
PHP_FALIAS(close,mysqli_stmt_close,NULL)
215217
PHP_FALIAS(errno,mysqli_stmt_errno,NULL)
216218
PHP_FALIAS(error,mysqli_stmt_error,NULL)

ext/mysqli/mysqli_nonapi.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
PHP_FUNCTION(mysqli_connect)
3535
{
3636
MYSQL *mysql;
37+
zval *object = getThis();
3738
char *hostname = NULL, *username=NULL, *passwd=NULL, *dbname=NULL, *socket=NULL;
3839
unsigned int hostname_len, username_len, passwd_len, dbname_len, socket_len;
3940
unsigned int port=0;
@@ -69,7 +70,11 @@ PHP_FUNCTION(mysqli_connect)
6970
RETURN_FALSE;
7071
}
7172

72-
MYSQLI_RETURN_RESOURCE(mysql, mysqli_link_class_entry);
73+
if (!object) {
74+
MYSQLI_RETURN_RESOURCE(mysql, mysqli_link_class_entry);
75+
} else {
76+
((mysqli_object *) zend_object_store_get_object(object TSRMLS_CC))->ptr = mysql;
77+
}
7378
}
7479
/* }}} */
7580

ext/mysqli/php_mysqli.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ PHP_FUNCTION(mysqli_send_query);
216216
PHP_FUNCTION(mysqli_slave_query);
217217
PHP_FUNCTION(mysqli_ssl_set);
218218
PHP_FUNCTION(mysqli_stat);
219+
PHP_FUNCTION(mysqli_stmt_affected_rows);
219220
PHP_FUNCTION(mysqli_stmt_close);
220221
PHP_FUNCTION(mysqli_stmt_errno);
221222
PHP_FUNCTION(mysqli_stmt_error);

0 commit comments

Comments
 (0)