Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

real_escape_string not accept numbers #5639

Closed
LaySoft opened this issue Dec 26, 2024 · 4 comments
Closed

real_escape_string not accept numbers #5639

LaySoft opened this issue Dec 26, 2024 · 4 comments
Labels

Comments

@LaySoft
Copy link

LaySoft commented Dec 26, 2024

If real_escape_string method using with connection handler provided by MysqliPool, and the method parameter is number, the following error occured:

Fatal error: Uncaught TypeError: mysqli::real_escape_string(): Argument #1 ($string) must be of type string, int given in @swoole/library/core/Database/MysqliProxy.php:50

@matyhtf
Copy link
Member

matyhtf commented Dec 30, 2024

Please provide the complete call stack. You need to modify the code so that when the value is an integer, you do not use Mysqli::real_escape_string(). This function only accepts string parameters and cannot handle integers.

@LaySoft
Copy link
Author

LaySoft commented Dec 30, 2024

The original mysql_real_escape_string works with numbers:

$conn = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB, MYSQL_PORT);

echo mysqli_real_escape_string($conn, 123);

But with the Swoole pool connection not:

use Swoole\Database\MysqliConfig;
use Swoole\Database\MysqliPool;

$config = (new MysqliConfig())
	->withHost(MYSQL_HOST)
	->withPort(MYSQL_PORT)
	->withUsername(MYSQL_USER)
	->withPassword(MYSQL_PASS)
	->withDbName(MYSQL_DB)
	->withCharset('utf8mb4');

$pool =  new MysqliPool($config, 5);

Co\run(function() use ($pool) {
	Co\go(function() use ($pool) {
		$conn = $pool->get();
		echo $conn->real_escape_string('456');
		echo $conn->real_escape_string(789);
	});
});

The first echo shows '456', but the second throws a fatal error.

@matyhtf
Copy link
Member

matyhtf commented Jan 3, 2025

@LaySoft This is because strict mode is enabled in the Swoole code. Try adding declare(strict_types=1); at the top of your file.

Please refer to the PHP documentation for the mysqli_real_escape_string function: https://www.php.net/manual/en/mysqli.real-escape-string.php. The first parameter must be of type string, not int.

In non-strict mode, PHP automatically performs type conversion, so no error is thrown. However, this results in an ineffective call since applying mysqli_real_escape_string to a number is meaningless.

@LaySoft
Copy link
Author

LaySoft commented Jan 3, 2025

Thank you, that makes sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants