rfc:mysqli_execute_parameters
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
rfc:mysqli_execute_parameters [2020/12/26 11:16] – Add note about mysqli::query() craigfrancis | rfc:mysqli_execute_parameters [2020/12/26 14:24] (current) – Added syntax highlighting dharman | ||
---|---|---|---|
Line 15: | Line 15: | ||
Using parameterised queries with //mysqli// is too complicated: | Using parameterised queries with //mysqli// is too complicated: | ||
- | | + | <code php> |
+ | $db = new mysqli(' | ||
| | ||
- | | + | $sql = ' |
- | + | ||
- | $name = ' | + | $name = ' |
- | $type = ' | + | $type = ' |
- | + | ||
- | $statement = $db-> | + | $statement = $db-> |
- | $statement-> | + | $statement-> |
- | $statement-> | + | $statement-> |
- | + | ||
- | $result = $statement-> | + | $result = $statement-> |
- | + | ||
- | while ($row = $result-> | + | while ($row = $result-> |
- | print_r($row); | + | print_r($row); |
- | } | + | } |
+ | </ | ||
Note that variables need to be used in // | Note that variables need to be used in // | ||
Line 42: | Line 44: | ||
Introduce // | Introduce // | ||
- | | + | <code php> |
- | + | $sql = ' | |
- | $parameters = [' | + | |
- | + | $parameters = [' | |
- | $result = $db-> | + | |
- | + | $result = $db-> | |
- | while ($row = $result-> | + | |
- | print_r($row); | + | while ($row = $result-> |
- | } | + | print_r($row); |
+ | } | ||
+ | </ | ||
We probably cannot change // | We probably cannot change // | ||
Line 58: | Line 62: | ||
Something like this: | Something like this: | ||
- | | + | <code php> |
- | | + | <?php |
- | | + | |
- | | + | class mysqli_rfc extends mysqli { |
+ | function execute($sql, | ||
$statement = mysqli_prepare($this, | $statement = mysqli_prepare($this, | ||
- | | + | |
$ref_types = ''; | $ref_types = ''; | ||
foreach ($parameters as $key => $value) { | foreach ($parameters as $key => $value) { | ||
- | | + | |
- | $ref_values[] = & | + | $ref_values[] = & |
} | } | ||
- | | + | |
array_unshift($ref_values, | array_unshift($ref_values, | ||
- | | + | |
call_user_func_array([$statement, | call_user_func_array([$statement, | ||
- | | + | |
$statement-> | $statement-> | ||
- | | + | |
return $statement-> | return $statement-> | ||
- | | ||
- | } | ||
- | | ||
} | } | ||
+ | } | ||
+ | </ | ||
Where //$sql// should be a [[https:// | Where //$sql// should be a [[https:// |
rfc/mysqli_execute_parameters.1608981373.txt.gz · Last modified: 2020/12/26 11:16 by craigfrancis