php - PDO which is the correct syntax to specify named parameters -
to find users in table, pdo prepared statements used:
$pdo = new pdo("mysql:host=localhost;dbname=mydatabase", "username", "password"); $stmt = $pdo->prepare("select surname, username users (username=:u or surname :sn);");
to specify named parameters here :u , :sn, both 2 ways give results.
first, colon preceeding variable name :u , :sn:
$stmt->execute(array( ":u" => "johndoe2" , ":sn" => "%super%" ));
or bare variable names u , sn:
$stmt->execute(array( "u" => "johndoe2" , "sn" => "%super%" ));
which syntax standard , preferred?
you can safely use either because binding mechanism prefix parameter name colon if 1 not found.
see https://github.com/php/php-src/blob/master/ext/pdo/pdo_stmt.c#l363
if (is_param && param->name[0] != ':') { char *temp = emalloc(++param->namelen + 1); temp[0] = ':'; memmove(temp+1, param->name, param->namelen); param->name = temp; } else { param->name = estrndup(param->name, param->namelen); }
i prefer use former feel i'm saving minuscule amount cycles.
Comments
Post a Comment