Skip to content

Commit b14f535

Browse files
committed
bindValue now uses try_convert_to_string as fallback if it is not any type expected
1 parent b558a18 commit b14f535

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

ext/pdo_mysql/mysql_statement.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,10 @@ static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_da
491491
mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_DOUBLE);
492492
break;
493493
default:
494-
PDO_DBG_RETURN(0);
494+
if (!try_convert_to_string(parameter)) {
495+
PDO_DBG_RETURN(0);
496+
}
497+
mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_VAR_STRING);
495498
}
496499

497500
PDO_DBG_RETURN(1);
@@ -531,7 +534,14 @@ static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_da
531534
PDO_DBG_RETURN(1);
532535

533536
default:
534-
PDO_DBG_RETURN(0);
537+
if (!try_convert_to_string(parameter)) {
538+
PDO_DBG_RETURN(0);
539+
}
540+
b->buffer_type = MYSQL_TYPE_STRING;
541+
b->buffer = Z_STRVAL_P(parameter);
542+
b->buffer_length = Z_STRLEN_P(parameter);
543+
*b->length = Z_STRLEN_P(parameter);
544+
PDO_DBG_RETURN(1);
535545
}
536546
#endif /* PDO_USE_MYSQLND */
537547
case PDO_PARAM_EVT_FREE:

ext/pdo_mysql/tests/gh13384.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
GH-13384 Fixed GH-13167 Fixed the behavior when an inappropriate value was passed to `bindValue`.
3+
--EXTENSIONS--
4+
pdo_mysql
5+
--SKIPIF--
6+
<?php
7+
require_once __DIR__ . '/inc/mysql_pdo_test.inc';
8+
MySQLPDOTest::skip();
9+
?>
10+
--FILE--
11+
<?php
12+
require_once __DIR__ . '/inc/mysql_pdo_test.inc';
13+
$db = MySQLPDOTest::factory();
14+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
15+
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
16+
17+
$db->exec('CREATE TABLE test_gh13384 (mode TINYINT)');
18+
19+
$stmt = $db->prepare('INSERT INTO test_gh13384 (mode) VALUES (?)');
20+
$stmt->bindValue(1, new DateTime(), PDO::PARAM_INT);
21+
22+
var_dump($stmt->execute());
23+
var_dump($db->errorInfo());
24+
var_dump($db->query('SELECT * FROM test_gh13384')->fetchAll());
25+
?>
26+
--CLEAN--
27+
<?php
28+
require_once __DIR__ . '/inc/mysql_pdo_test.inc';
29+
$db = MySQLPDOTest::factory();
30+
$db->exec('DROP TABLE IF EXISTS test_gh13384');
31+
?>
32+
--EXPECTF--
33+
Fatal error: Uncaught Error: Object of class DateTime could not be converted to string in %s
34+
Stack trace:
35+
#0 %s: PDOStatement->execute()
36+
#1 {main}
37+
thrown in %s

0 commit comments

Comments
 (0)