I'm foolishly writing OO PHP and each object corresponds to a table in the MySQL database. There's an easy way to load the record into memory, i.e., with something like $this->dbrecord=$result->fetch_assoc();
, but if I want to do the opposite and put the array back into the database, there doesn't seem to be an easy way to do this, especially since you can't use parameters for column names, that is to say, something like
$query=$sql->prepare("UPDATE ? SET ?=?");
$query->bind_param("sss", $this->table, $key, $value);
foreach ($dbrecord as $key => $value){
$query->execute();
}
wouldn't work, so I would have to use unsafe and ugly query-splicing where I put the individual values into a string and then pass the string to the MySQL server.
My question is: is there a way to do this elegantly? If there were something like mysqli_stmt::put_array($array), that would be ideal, but if not, there must be some way to write a general-purpose function that does something like this, right? Something I haven't thought of?