Accept table/column name as a parameter in PDO

Wrapper to allow binding tables/columns names to a PDO statement query. Read more.

php
fork add to bundle source
function pdo_execute_dynamic($sql, $bind) { $handle = new PDO('mysql:host=127.0.0.1;dbname=db', 'user', 'pwd'); foreach ( $bind as $key => $value ) { # pass table/column names starting with a "#" symbol if ( strpos($key, '#') === 0 ) { unset($bind[$key]); $sql = str_replace($key, "`{$value}`", $sql); } } $st = $handle->prepare($sql); $st->execute($bind); return $st; }

usage

load_function('pdo_execute_dynamic'); $statement = pdo_execute_dynamic( 'SELECT * FROM #table WHERE #col = :value', ['#table' => 'users', '#col' => 'email', ':value' => 'anyone@app.function.codes'] ); print_r($statement->fetch(PDO::FETCH_ASSOC)); // Array // ( // [email] => anyone@app.function.codes // [password] => 123 // )
← how to use this in app?
create log ...inout