php / php/php-src

Problem with mysqli_options and persistent connections in mysqli.

Open
#7,808 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: mysqli Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

According to the documentation https://www.php.net/manual/en/mysqli.options.php
mysqli_options() should be called after mysqli_init() and before mysqli_real_connect().

But, when using persistent connections and the connection in the pool is broken,
when creating a new connection, the MYSQLI_OPT_INT_AND_FLOAT_NATIVE option is lost.

Setting the option AFTER calling real_connect solves the problem

The following code:

<?php
\mysqli_report(MYSQLI_REPORT_ERROR);
function connect(): mysqli {
	$mysqli = mysqli_init();
	$mysqli->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
	$mysqli->real_connect('p:127.0.0.1', 'user', 'password');

	return $mysqli;
}

$mysqli1 = connect();
var_dump($mysqli1->query("SELECT 1")->fetch_row()[0]); // int
@$mysqli1->query("KILL {$mysqli1->thread_id}");
unset($mysqli1);

$mysqli2 = connect();
var_dump($mysqli2->query("SELECT 2")->fetch_row()[0]); // string, but int is expected

// next ok
$mysqli2->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
var_dump($mysqli2->query("SELECT 3")->fetch_row()[0]); // int

Resulted in this output:

int(1)
string(1) "2"
int(3)

But I expected this output instead:

int(1)
int(2)
int(3)
PHP Version

PHP 8.0.14, 8.1.1

Operating System

Debian 9.13, Ubuntu 21.10

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The report identifies mysqli_init(), mysqli_options(), mysqli_real_connect(), and persistent p: connections; start by reproducing the supplied PHP script with the documented option order. No source file or test is named, so trace how the option is handled when a broken pooled connection is replaced. Done means the option remains effective and the second query returns an integer.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, php
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.