Scripts

mysqli_query_params()

Een (ongeteste) hulpfunctie om snel prepared statements te doen via een MySQLi-connectie. Niet bedoeld voor gebruik i.c.m. PDO.

mysqli_query_params.php
<?php

/**
 * Hulpfunctie voor uitvoeren prepared statements
 * @note als pg_query_params() maar dan met array
 * @param resource $connection
 * @param string $query
 * @param array $params
 * @return array
 */

function mysqli_query_params($connection, string $query, array $params) : array
{
  if (FALSE === $mysqli_stmt = mysqli_prepare($connection, $query)) {return FALSE;}
  $format = '';
  foreach ($params as $i => $v) {
    if (is_string($v)) {$format .= 's';}
    elseif (is_int($v)) {$format .= 'i';}
    elseif (is_float($v)) {$format .= 'd';}
    elseif (is_null($v)) {$format .= 's';}
    elseif (is_bool($v)) {$format .= 'i';$params[$i] = boolval($v);}
    else {return FALSE;}
  }
  $args = [$format];
  foreach (array_keys($params) as $i) {$args[] =& $params[$i];}
  if (FALSE === @call_user_func_array([$stmt, 'bind_param'], $args)) {return FALSE;}
  if (FALSE === $stmt->execute()) {return FALSE;}
  $res = $stmt->get_result();
  $stmt->close();
  $array = [];
  if (FALSE !== $res and 0 < $oResultaat->num_rows) {
    while ($row = $res->fetch_assoc()) {$array[] = $row;}
    $res->close();
  }
  return $array;
}

Reacties

0
Nog geen reacties.