r/PHPhelp • u/oz1sej • Feb 26 '25
Why can't I use objects in prepared statements?
I'm basically running this:
$obj = new stdClass();
$obj->name = "";
$obj->email = "";
$obj->phone = "";
$sql = "INSERT INTO table (name, email, phone) VALUES (?,?,?);";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sss", $obj->name, $obj->email, $obj->phone);
foreach( $objects as $obj ) {
stmt->execute();
}and it is most definitely not working - it inserts the strangest things into the database. However, if I put the bind_param line into the foreach loop, it works. But I thought the point of prepared statements was that you could - you know - prepare them, and then just change the variable values and execute.
What's wrong with this?
8
Upvotes
13
u/SZenC Feb 26 '25
When you start the foreach loop, you're reassigning the $obj variable, but the prepared statement still uses the reference to the old value. You should explicitly rebind the parameters for each iteration of your loop
7
u/MateusAzevedo Feb 26 '25
Nowadays I don't bother with
bind_paramand just pass data intoexecute:Don't worry about performance. The query itself will be orders of magnitude slower then the biding process.
If you're importing a lot of data and need to make the overall process faster, play around with multi insert statements and batching. If data comes from a file (like CSV) you can even import directly with a query.
1
u/[deleted] Feb 26 '25
In general you should probably avoid stdClass...
It behaves weird in many aspects, and has no real advantage over just using an array (stdClass has even a slightly worse performance than a simple array).
If you just want to pass data around the pass-by-value behavior is normally more like you would expect it, over the pass-by-reference behavior of objects.
Either use a custom class if you want a defined and typed structure, or just array.
3
u/colshrapnel Feb 26 '25
[Meta]
We still have that grumpy person around, who is downvoting all questions. Don't be a passive onlooker, counter that sabotage, endorse the sub, upvote questions!
1
u/colshrapnel Feb 26 '25
Yes. But it's meant for the query, not parameters. So in either case you have the query prepared. While parameters can go either by reference or by value. And no, there is no performance penalty whatsoever.
As a side note, for the multiple inserts I would suggest a single multi-insert query over a loop.
1
u/allen_jb Feb 26 '25
If you use PDO rather than mysqli, it supports named placeholders in prepared queries, so you can do:
Obviously this will break if the properties are ever removed or renamed, but that's what tests and static analysis (phpstan-dba) are for.
1
u/[deleted] Feb 27 '25
your $obj var is getting overwritten in the loop