using this function it should take the query and check results against the type (integer, string, blob, decimal), if the result is correct type return it, if not then return error or nothing
it executes correctly if type is correct, but if type is wrong it returns all results instead... not sure where it went wrong
any help is always appreciated!
thanks
<?php
$sql = "SELECT * FROM user WHERE last_name=? && first_name=?";
$typeDef = array("s","s");
$data = array("jones","jim");
$query = sql_query($sql, $typeDef, $data);
echo '<pre>'; print_r($query); echo '</pre>';
function sql_query($sql, $typeDef, $data) {
$mysqli = mysqli_con();
$type = ''; $multiQuery = '';
foreach($typeDef AS $val) { $type .= $val; }
if($stmt = $mysqli->prepare($sql)) {
if(count($data) == count($data, 1)) {
$data = array($data);
$multiQuery = FALSE;
} else {
$multiQuery = TRUE;
}
if($type) {
$bindParams = array();
$bindParamsReferences = array();
$bindParams = array_pad($bindParams,(count($data,1)-count($data))/count($data), "");
foreach($bindParams as $key => $value) {
$bindParamsReferences[$key] = &$bindParams[$key];
}
array_unshift($bindParamsReferences,$type);
$bindParamsMethod = new ReflectionMethod('mysqli_stmt', 'bind_param');
$bindParamsMethod->invokeArgs($stmt, $bindParamsReferences);
}
$result = array();
foreach($data as $queryKey => $query) {
foreach($bindParams as $paramKey => $value) {
$bindParams[$paramKey] = $query[$paramKey];
}
$queryResult = array();
if(mysqli_stmt_execute($stmt)) {
$resultMetaData = mysqli_stmt_result_metadata($stmt);
if($resultMetaData) {
$stmtRow = array();
$rowReferences = array();
while ($field = mysqli_fetch_field($resultMetaData)) {
$rowReferences[] = &$stmtRow[$field->name];
}
mysqli_free_result($resultMetaData);
$bindResultMethod = new ReflectionMethod('mysqli_stmt', 'bind_result');
$bindResultMethod->invokeArgs($stmt, $rowReferences);
while(mysqli_stmt_fetch($stmt)) {
$row = array();
foreach($stmtRow as $key => $value) {
$row[$key] = $value;
}
$queryResult[] = $row;
}
mysqli_stmt_free_result($stmt);
} else {
$queryResult[] = mysqli_stmt_affected_rows($stmt);
}
} else {
$queryResult[] = FALSE;
}
$result[$queryKey] = $queryResult;
}
mysqli_stmt_close($stmt);
} else {
$result = FALSE;
}
if($multiQuery) {
return $result;
} else {
return $result[0];
}
}
?>