I
I
iSize1ce2015-11-09 05:20:54
PHP
iSize1ce, 2015-11-09 05:20:54

How to make such a function?

function mysqli_query_function($mysqli, $query, $vars) {
  if(!isset($mysqli)) {
    return FALSE;
  }
  if(!isset($query)) {
    return FALSE;
  }
  if(!isset($vars)) {
    return FALSE;
  }
  $stmt = $mysqli->prepare($query);
  $stmt->bind_param('ssssssssss', $var[0], $var[1] .......);
  $stmt->execute();
}

How to make bind_param() generation? $vars is an array. The output should look like this:
$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);

$query = 'UPDATE `users` SET `user` = ?, `id` = ? WHERE `login` =  ?';

$vars['user'] = 'user';
$vars['id'] = 'id';
$vars['login'] = 'login';

mysqli_query_function($mysqli, $query, $vars);

if(!isset($mysqli)) {
  return FALSE;
}
if(!isset($query)) {
  return FALSE;
}
if(!isset($vars)) {
  return FALSE;
}
$stmt = $mysqli->prepare('UPDATE `users` SET `user` = ?, `id` = ? WHERE `login` =  ?');
$stmt->bind_param('sss', $var['user'], $var['id'], $var['login']);
$stmt->execute();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
index0h, 2015-11-09
@index0h

What only people do not go to not use PDO.
In the case of mysqli, you will have to store the key-position bind somewhere, but this is a road to nowhere.

S
Stalker_RED, 2015-11-09
@Stalker_RED

What do you mean by "do generate bind_param()"?
It already exists php.net/manual/ru/mysqli-stmt.bind-param.php

$vars = [
  'user'=>'Mike',
  'id'=>123,
  'login'=>'Agent M'
];
$types = str_pad('', count($vars), 's'); // генерируем строку 'sss' нужной длинны
$params = array_unshift($var, $types); // делаем ее первым параметром
$stmt = $mysqli->prepare('UPDATE `users` SET `user` = ?, `id` = ? WHERE `login` =  ?');
$stmt->execute();
call_user_func_array ([$stmt, 'bind_param'], $params); // вызываем метод с этими параметрами
$stmt->execute();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question