I have a database class which contains a function called query, basically this function handles all the requests to the database etc.
When binding the parameters, i would like to know if it was succesful or not. For example, say i tried to bind_params on array('i', 'This is not an integer') then this shouldn't work as the value is not an integer. Any ideas on how i could implement such code into my function below.
PHP Code:
function query($sql, $params, $close){ $mysqli = new mysqli("host", "user", "pass", "name"); $stmt = $mysqli->prepare($sql) or die ("Failed to prepared the statement!"); call_user_func_array(array($stmt, 'bind_param'), $this->refValues($params)); $stmt->execute(); if($close){ $result = $mysqli->affected_rows; } else { $meta = $stmt->result_metadata(); while ( $field = $meta->fetch_field() ) { $parameters[] = &$row[$field->name]; } call_user_func_array(array($stmt, 'bind_result'), $this->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){ 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; }
|