- ⌂ query
- Methods
query::bind()
Associate (aka bind) one or more values to their placeholders in the query. Two types of placeholders are supported.
- ? - Unnamed placeholders are specified as a question mark (?) in the query. For example, "SELECT * FROM Contact WHERE Age >= ?". This type of placeholder is handy for short and simple queries.
- :name - Named placeholders are specified with a colon (:) followed by a variable name (must start with a letter and only contain upper- and lower-case letters, numbers and underscores). This type of placeholder is handy when more than a few placeholders are needed, or bind() may add them out of order from the order they appear in the query.
The options parameter can also be used to bind values with repo::get().
Prototype
bind(mixed $value): query
bind(mixed $key, mixed $value): query
bind(array $all_values): query
Parameters
The following parameters may be passed in any of the forms listed above.
- value - Bind an additional value to be associated with the next ? placeholder, or $key if specified.
- key - Bind an additional value to be associated with a placeholder name (string) or index (int).
- all_values - array containing values to bind to ? placeholders, or associative array containing key/value pairs to bind to named placeholders. Keys can be numeric indexes. The all_values array will completely replace any existing bindings, rather than append like when a single value parameter or key and value parameters are provided.
Values must be a single primitive value, not a string of multiple values, such as comma-separated list of values. For example, the following will not return the expected results:
$repo->query()->where('OID IN (?)')->bind('123, 456, 789');
One of the following must be used instead:
$repo->query()->where('OID IN (?, ?, ?)')->bind([123, 456, 789]);
$repo->query()->where('OID IN (:OID1, :OID2, :OID3)')->bind(['OID1'=>123, 'OID2'=>456, 'OID3'=>789]);
$repo->query()->where('OID IN (?)')->bind('123, 456, 789');
One of the following must be used instead:
$repo->query()->where('OID IN (?, ?, ?)')->bind([123, 456, 789]);
$repo->query()->where('OID IN (:OID1, :OID2, :OID3)')->bind(['OID1'=>123, 'OID2'=>456, 'OID3'=>789]);
Return
The updated query object is returned. This is handy for chaining additional methods.
Example
$name = 'Jane';
$age = 18;
// use ? placeholders
$results = $repo->query()
->where('Name=? AND Age>?')
->bind([$name, $age])
->get();
// use named placeholders
$results = $repo->query()
->where('Name=:name AND Age>:age')
->bind(['name'=>$name, 'age'=>$age])
->get();