bind_param in mysql from JSON
<p>I'd like to create a 'one fits them all' function for updating an SQL database.</p>

<p>Imagine I have a database table with columns : </p>

<ul>
<li>id (int)</li>
<li>col1 (varchar)</li>
<li>col2 (varchar)</li>
<li>col3 (varchar)</li>
<li>col4 (varchar)</li>
<li>col5 (int)</li>
</ul>

<p>Now the php script receives some JSON like this : </p>

<pre><code>{ id : 5,
col3 : "somevalue",
col5 : 17 }
</code></pre>

<p>So I would like to recreate an update function with a clean prepared  MySqli statement:</p>

<pre><code>$stmt = $db-&gt;prepare("UPDATE `table` SET `col3` = ?, `col5` = ? WHERE `id` = ?;");
$stmt-&gt;bind_param("sii", $obj-&gt;col3, $obj-&gt;col5, $obj-&gt;id);
$stmt-&gt;execute();
</code></pre>

<p>Seems quite simple at first, but I don't get how I'm supposed to dynamically recreate the argument list inside the bind_param method. It does not take an array, but all values in different parameters..</p>

<p><strong>Question :</strong> How to recreate the bind_param without knowing on the forehand how many columns and what kind they are.</p>

<p>listing the kind of variable is simple, iterating over all object parameters and creating a string containing "sii" in this case..</p>

<p>json object properties always reflect table's columns.</p>