Skip to content

MySQL Statement has a empty query result when the response field has changed, also Segmentation fault #11550

Closed
@Yurunsoft

Description

@Yurunsoft

Description

The following code:

<?php

declare(strict_types=1);

function getPDO(): PDO
{
    return new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'root', [
        PDO::ATTR_ERRMODE           => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_EMULATE_PREPARES  => false,
    ]);
}

function testPDO(): void
{
    $pdo = getPDO();
    $pdo->exec(<<<'SQL'
    DROP TABLE IF EXISTS `test`
    SQL);
    $pdo->exec(<<<'SQL'
    CREATE TABLE `test`  (
      `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
      `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
      PRIMARY KEY (`id`) USING BTREE,
      INDEX `name`(`name`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
    SQL);
    $pdo->exec(<<<'SQL'
    INSERT INTO `test` (`name`) VALUES ('test1');
    SQL);

    $pdo2 = getPDO();
    $stmt = $pdo2->prepare('select * from test');
    var_dump('PDO-1:', $stmt->execute(), $stmt->fetchAll());

    $stmt->closeCursor(); // Optional. Segmentation fault (core dumped)

    $pdo->exec(<<<'SQL'
    ALTER TABLE `test`
    ADD COLUMN `a` varchar(255) NOT NULL DEFAULT '';
    SQL);

    var_dump('PDO-2:', $stmt->execute(), $stmt->fetchAll());
}

function getMysqli(): mysqli
{
    return new mysqli('127.0.0.1', 'root', 'root', 'test');
}

function testMysqli(): void
{
    $mysqli = getMysqli();
    $mysqli->query(<<<'SQL'
    DROP TABLE IF EXISTS `test`
    SQL);
    $mysqli->query(<<<'SQL'
    CREATE TABLE `test`  (
      `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
      `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
      PRIMARY KEY (`id`) USING BTREE,
      INDEX `name`(`name`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
    SQL);
    $mysqli->query(<<<'SQL'
    INSERT INTO `test` (`name`) VALUES ('test1');
    SQL);

    $mysqli2 = getMysqli();
    $stmt = $mysqli2->prepare('select * from test');
    var_dump('mysqli-1:', $stmt->execute(), $stmt->get_result()->fetch_all());

    $mysqli->query(<<<'SQL'
    ALTER TABLE `test`
    ADD COLUMN `a` varchar(255) NOT NULL DEFAULT '';
    SQL);

    var_dump('mysqli-2:', $stmt->execute(), $stmt->get_result()->fetch_all());
}

testMysqli();
testPDO();

Resulted in this output:

string(9) "mysqli-1:"
bool(true)
array(1) {
  [0]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    string(5) "test1"
  }
}
string(9) "mysqli-2:"
bool(true)
array(0) {
}
string(6) "PDO-1:"
bool(true)
array(1) {
  [0]=>
  array(4) {
    ["id"]=>
    int(1)
    [0]=>
    int(1)
    ["name"]=>
    string(5) "test1"
    [1]=>
    string(5) "test1"
  }
}
Segmentation fault (core dumped)

But I expected this output instead:

string(9) "mysqli-1:"
bool(true)
array(1) {
  [0]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    string(5) "test1"
  }
}
string(9) "mysqli-2:"
bool(true)
array(1) {
  [0]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    string(5) "test1"
    [2]=>
    string(0) ""
  }
}
string(6) "PDO-1:"
bool(true)
array(1) {
  [0]=>
  array(4) {
    ["id"]=>
    int(1)
    [0]=>
    int(1)
    ["name"]=>
    string(5) "test1"
    [1]=>
    string(5) "test1"
  }
}
string(6) "PDO-2:"
bool(true)
array(1) {
  [0]=>
  array(6) {
    ["id"]=>
    int(1)
    [0]=>
    int(1)
    ["name"]=>
    string(5) "test1"
    [1]=>
    string(5) "test1"
    ["a"]=>
    string(0) ""
    [2]=>
    string(0) ""
  }
}

This bug affects mysqli, pdo_mysql.

I will fix this issue later.

PHP Version

PHP 8.1.21

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions