Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No... this was definitely harder than PDO with any array because of how <a href="http://php.net/manual/en/mysqli-stmt.bind-param.php" rel="nofollow noreferrer">mysqli_stmt_bind_param()</a> works... and this works fine by changing <code>$array</code> to removing/adding data for other columns.</p> <pre><code>$mysqli = new mysqli('localhost', 'root', 'password', 'test'); $array = array("name"=&gt;"pineapple", "color"=&gt;"purple"); $table_name = "fruit"; insert_data($mysqli, $array, $table_name); function insert_data($mysqli, $array, $table_name) { $placeholders = array_fill(0, count($array), '?'); $keys = array(); $values = array(); foreach($array as $k =&gt; $v) { $keys[] = $k; $values[] = !empty($v) ? $v : null; } $query = "insert into $table_name ". '('.implode(', ', $keys).') values '. '('.implode(', ', $placeholders).'); '; // insert into fruit (name, color) values (?, ?); $stmt = $mysqli-&gt;prepare($query); // create a by reference array... $params = array(); foreach ($array as &amp;$value) { $params[] = &amp;$value; } $types = array(str_repeat('s', count($params))); $values = array_merge($types, $params); /* $values = Array ( [0] =&gt; ss [1] =&gt; pineapple [2] =&gt; purple ) */ call_user_func_array(array($stmt, 'bind_param'), $values); $success = $stmt-&gt;execute(); if ($success) { print "it worked..."; } else { print "it did not work..."; } } </code></pre> <p>I got some help from these SO posts:<br> - <a href="https://stackoverflow.com/a/15933696/623952">https://stackoverflow.com/a/15933696/623952</a><br> - <a href="https://stackoverflow.com/a/6179049/623952">https://stackoverflow.com/a/6179049/623952</a> </p> <hr> <p>So... in <code>$stmt-&gt;bind_param()</code> the first parameter is a string that has one char for each parameter passed in. And that char represents the parameter data type. In the example above, both of the two parameters are strings so it becomes <code>ss</code>. A string is always assumed in the example above, too. </p> <p>I found this chart in the <code>bind_param()</code> documentation: </p> <p><strong>types</strong><br> A string that contains one or more characters which specify the types for the corresponding bind variables:</p> <pre><code>Type specification chars Character Description i corresponding variable has type integer d corresponding variable has type double s corresponding variable has type string b corresponding variable is a blob and will be sent in packets </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload