I can seem to get this code to work, I receive the error "Commands out of sync; you can't run this command now" when it gets to " $stmt->execute(); "
I'm using eclipse to debug it but I'm more familiar with procedural code, it appears to be running ok upto the stmt->execute part.
the code is below
function execSQL($sql, $params, $close){
$mysqli = new mysqli("localhost", "web_user", "jaitec", "ebay");
$stmt = $mysqli->prepare($sql) or die ("Failed to prepared the statement!");
call_user_func_array(array($stmt, 'bind_param'), refValues($params));
$stmt->execute();
if($close){
$result = $mysqli->affected_rows;
} else {
$result = $mysqli->affected_rows;
$meta = $stmt->result_metadata();
while ( $field = $meta->fetch_field() ) {
$parameters[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), refValues($parameters));
while ( $stmt->fetch() ) {
$x = array();
foreach( $row as $key => $val ) {
$x[$key] = $val;
}
$results[] = $x;
}
$result = $results;
}
$stmt->close();
$mysqli->close();
return $result;
}
function refValues(&$arr) // Changed $arr to reference for PHP v7.1.7
{
if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
{
$refs = array();
foreach($arr as $key => $value)
$refs[$key] = &$arr[$key];
return $refs;
}
return $arr;
}
$_REQUEST['term'] = "m8 knob";
$part_1 = "knob";
$part_2 = "m8";
execSQL("SELECT * FROM web_search WHERE part_no LIKE ? AND part_no LIKE ?", array('ss', $part_1, $part_2), false);