Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

laraguy25's avatar

Invalid parameter number using raw query, BUT THE CORRECT # OF PARAMS ARE BOUND

I'm receiving an error when trying to run a SQL statement with whereRaw()

Error: Invalid parameter number

MyModel::whereRaw("status_id_one = :statusId OR status_id_two = :statusId",[':statusId' => $statusId])->count();

:statusId is the only value bound in the query two times same value.. why isn't this working?

0 likes
4 replies
jlrdw's avatar

Lookup usage of the IN operator.

Edit:

You can also work it out in an if statement. Or prior to the query figure out which one.

Also check the docs I think there is an orWhere clause.

JussiMannisto's avatar

I guess duplicate placeholders don't work in the query builder. With straight PDO your idea should work.

You can bind the same parameter twice with a different placeholder name. You can also omit colons from the placeholder names when binding values:

$count = MyModel::whereRaw('status_id_one = :id1 OR status_id_two = :id2', [
	'id1' => $statusId, 
	'id2' => $statusId, 
])->count();

But there's no reason to use whereRaw here:

$count = MyModel::where('status_id_one', $statusId)
			->orWhere('status_id_two', $statusId)
			->count();
laraguy25's avatar

@jussimannisto I appreciate you taking the time to answer my question and perhaps the example code is rather confusing.

I'm not looking for a better way to write the query, I need to figure out why I'm getting an "Error: Invalid parameter number" when trying to bind the same value more then once on a raw query.

JussiMannisto's avatar

@laraguy25 You should always try googling first. I found this issue from the tracker and this paragraph from the PHP docs:

You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name more than once in a prepared statement, unless emulation mode is on.

Please or to participate in this conversation.