Bind all params automatically in Mysqli

Detecting types of params and binding them with Mysqli bind() function. Read more.

php
fork add to bundle source
function mysqli_bind_all($sql, $bind = []) { $db = mysqli_connect("127.0.0.1", "user", "pwd", "db"); if ( preg_match_all('/:([a-z_0-9]+)/', $sql, $matches) ) { $types = ''; foreach ( $matches[1] as $key ) { $sql = str_replace(':' . $key, '?', $sql); $value = $bind[':' . $key]; if (is_int($value)) { $types .= 'i'; } elseif (is_float($value)) { $types .= 'd'; } elseif (is_string($value)) { $types .= 's'; } else { $types .= 'b'; } } } $stmt = $db->prepare($sql); if ( $types ) { $params = [$types]; foreach ( $bind as $key => $value ) { $$key = $value; $params[] = &$$key; } call_user_func_array([$stmt, 'bind_param'], $params); } $stmt->execute(); $result = $stmt->get_result(); return $result; }

usage

load_function('mysqli_bind_all'); $result = mysqli_bind_all( 'SELECT * FROM users WHERE password = :password AND email = :email', [':password' => '123', 'email' => 'anyone@app.function.codes'] ); $user = $result->fetch_assoc(); print_r($user); // Array // ( // [email] => anyone@app.function.codes // [password] => 123 // )
← how to use this in app?
create log ...inout