amphp / mysql

An async MySQL client for PHP, optimizing database interactions with efficient non-blocking capabilities. Perfect for responsive, high-performance applications.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can we use Named parameters in SQL queries with amphp\mysql package?

raghuveer opened this issue · comments

While I am able to use question marks in prepared statements, I am getting error when I try to use named parameters when making insert query

my code using question marks (worked):

$sql = "INSERT INTO site_members (lastname, firstname) VALUES (?, ?)";
$statement = yield $pool->prepare($sql);
$result = yield $statement->execute(['Doe', 'John']);
$id = $result->getLastInsertId();
echo "id: " . $id . "\n";

code with named parameters:

$array = array(':lastname' => 'Doe', ':firstname' => 'John');
				
$sql = "INSERT INTO site_members (lastname, firstname) VALUES (:lastname, :firstname)";
$statement = yield $pool->prepare($sql);
$result = yield $statement->execute($array);
$id = $result->getLastInsertId();
echo "id: " . $id . "\n";

when I try using named parameters, as shared in the above, I get the following error:

PHP Fatal error:  Uncaught Error: Named parameter 'lastname' missing for executing prepared statement in /home/user/public_html/vendor/amphp/mysql/src/ConnectionStatement.php:124
Stack trace:
#0 /home/user/public_html/vendor/amphp/sql-common/src/PooledS                                                                                      tatement.php(64): Amp\Mysql\ConnectionStatement->execute()
#1 [internal function]: Amp\Sql\Common\PooledStatement->Amp\Sql\Common\{closure}                                                                                                             ()
#2 /home/user/public_html/vendor/amphp/amp/lib/Coroutine.php(                                                                                                             67): Generator->current()
#3 /home/user/public_html/vendor/amphp/amp/lib/functions.php(                                                                                                             96): Amp\Coroutine->__construct()
#4 /home/user/public_html/vendor/amphp/sql-common/src/PooledS                                                                                                             tatement.php(72): Amp\call()
#5 /home/user/public_html/vendor/amphp/sql-common/src/Stateme                                                                                                           ntPool.php(105): Amp\Sql\Common\PooledStatement->execute()
#6 [internal function]: Amp\Sql\Common\StatementPool->Amp\Sql\Common\{closure}()
#7 /home/user/public_html/vendor/amphp in /home/user/public_html/vendor/amphp/mysql/src/ConnectionStatement.php on line 12                                                                                                             4

The issue you're facing appears, because you're prefixing your array of parameters with a colon, so use $array = array('lastname' => 'Doe', 'firstname' => 'John'); instead of $array = array(':lastname' => 'Doe', ':firstname' => 'John');.