A mysqli_stmt
does not have a query_params()
function, I had to write my own. The parameter arry is bound to the statement with bind_param()
. I need to specify the variable types dynamically. I could do that with something like:
$sType = '';
foreach ($aParameters as $iIndex => $mParameter) {
if (is_string($mParameter)) {$sType .= 's';}
elseif ( is_int($mParameter)) {$sType .= 'i';}
elseif ( is_float($mParameter)) {$sType .= 'd';}
elseif ( is_null($mParameter)) {$sType .= 's';}
elseif ( is_bool($mParameter)) {
$sType .= 'i';
$aParameters[$iIndex] = boolval($mParameter);}
else {
// trow new Exception(...);
}
}
But as it turns out, mysql/mariadb will send booleans, integers and floats fine as strings, where the database server will happily cast them to the corresponding data type of the column. It seems like I could just skip this step and send every parameter as a string by default.
Are there any reaons to specify another data type than "s"
for each parameter?
EDIT: I just found this SO topic which shows how to use the "b" type and mysqli_stmt::send_long_data
when the binary packet would exceed the max_allowed_packet
setting. I also have read that it will improve performance over solutions that employ bin2hex()
to turn send a byte string as text.