]> granicus.if.org Git - php/commitdiff
Fixed error reporting in mysqli_stmt::__construct
authorDharman <tekiela246@gmail.com>
Tue, 24 Nov 2020 23:02:04 +0000 (23:02 +0000)
committerNikita Popov <nikita.ppv@gmail.com>
Wed, 25 Nov 2020 15:27:41 +0000 (16:27 +0100)
For the sake of simplicity, I've synchronized the implementation
with PHP 8, which means null values are also accepted.

Closes GH-6454.

ext/mysqli/mysqli.c
ext/mysqli/tests/063.phpt

index dcc5a16d5ec35afa7b8f774b193cf7a186b16bbc..c4dbcb251a668861e09ffa0e93c2912d32f6f9e9 100644 (file)
@@ -1041,51 +1041,38 @@ PHP_FUNCTION(mysqli_stmt_construct)
        zval                            *mysql_link;
        MY_STMT                         *stmt;
        MYSQLI_RESOURCE         *mysqli_resource;
-       char                            *statement;
+       char                            *statement = NULL;
        size_t                                  statement_len;
 
-       switch (ZEND_NUM_ARGS())
-       {
-               case 1:  /* mysql_stmt_init */
-                       if (zend_parse_parameters(1, "O", &mysql_link, mysqli_link_class_entry)==FAILURE) {
-                               return;
-                       }
-                       MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
-
-                       stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT));
-
-                       stmt->stmt = mysql_stmt_init(mysql->mysql);
-               break;
-               case 2:
-                       if (zend_parse_parameters(2, "Os", &mysql_link, mysqli_link_class_entry, &statement, &statement_len)==FAILURE) {
-                               return;
-                       }
-                       MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
-
-                       stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT));
-
-                       if ((stmt->stmt = mysql_stmt_init(mysql->mysql))) {
-                               mysql_stmt_prepare(stmt->stmt, (char *)statement, statement_len);
-                       }
-               break;
-               default:
-                       WRONG_PARAM_COUNT;
-               break;
+       if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|s!", &mysql_link, mysqli_link_class_entry, &statement, &statement_len) == FAILURE) {
+               return;
        }
+       MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
 
-       if (!stmt->stmt) {
+       stmt = (MY_STMT *) ecalloc(1, sizeof(MY_STMT));
+
+       if (!(stmt->stmt = mysql_stmt_init(mysql->mysql))) {
                efree(stmt);
                RETURN_FALSE;
        }
+
 #ifndef MYSQLI_USE_MYSQLND
        ZVAL_COPY(&stmt->link_handle, mysql_link);
 #endif
 
        mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
        mysqli_resource->ptr = (void *)stmt;
-       mysqli_resource->status = (ZEND_NUM_ARGS() == 1) ? MYSQLI_STATUS_INITIALIZED : MYSQLI_STATUS_VALID;
+       mysqli_resource->status = MYSQLI_STATUS_INITIALIZED;
 
        MYSQLI_REGISTER_RESOURCE_EX(mysqli_resource, getThis());
+
+       if (statement) {
+               if(mysql_stmt_prepare(stmt->stmt, statement, statement_len)) {
+                       MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
+                       RETURN_FALSE;
+               }
+               mysqli_resource->status = MYSQLI_STATUS_VALID;
+       }
 }
 /* }}} */
 
index 26bfe48ee54b2cb50513e5a6317ddc688d13d8f0..b76b2c99855af24623573ad4992e0111d653eb05 100644 (file)
@@ -16,9 +16,18 @@ require_once('skipifconnectfailure.inc');
     $stmt->bind_result($foo);
     $stmt->fetch();
     $stmt->close();
-    $mysql->close();
-
     var_dump($foo);
+
+    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
+    try {
+        // an exception should be thrown from prepare (i.e. constructor) not from execute
+        $stmt = new mysqli_stmt($mysql, "SELECT invalid FROM DUAL");
+    } catch(mysqli_sql_exception $e) {
+        echo $e->getMessage()."\n";
+    }
+
+    $mysql->close();
 ?>
 --EXPECT--
 string(3) "foo"
+Unknown column 'invalid' in 'field list'