This isn't correct here:
$getErrors = db_query(MySQLi $mysqli, "SELECT * FROM `errors` ORDER BY `id` DESC");. You cannot provide a datatype hint during the calling of the function; I mean the signature of the function can accept the typehint:
PHP Code:
function db_query(MySQLi $mysqli, $qry);
You are correct, the error for mysql_num_rows exists since you cannot combine mysqli and mysql libraries. I also don't recommend using the function calls since they differ between mysqli_stmt and mysqli_result objects. Instead, use them in object oriented fashion as they both share the same property name. Unfortunately, there is no superclass governing both mysqli_stmt and mysqli_result, so you cannot typehint:
PHP Code:
function count_rows($result)
{
if (!($result instanceof mysqli_Stmt) || !($result instanceof mysqli_result))
{
throw new InvalidArgumentException('Cannot only retrieve records from mysqli resultset object');
}
return $result->num_rows;
}
That would work with both mysqli_result and mysqli_stmt objects. Of course a custom function is optional.
-> is object oriented de-reference operator. It resolves an object to a property or method of the class' instance. To use in context as I've indicated with the third parameter of the method, that would be like so:
PHP Code:
function db_query(MySQLi $con, $sQry, array $params = array())
{
if (!empty($params))
{
// bind this
// This is an absolute pain in mysqli btw:
if ($stmt = $mysqli->prepare($sQry))
{
$rf = new ReflectionMethod($stmt, 'bind_param');
$aUseParams = array();
foreach ($params AS $itm)
{
$aUseParams[] = &$itm;
}
// this is lazy, but I'ma do it anyway
$sParamTypes = str_repeat('s', count($aUseParams));
$rf->invokeArgs($stmt, array($sParamTypes, implode(', ', $aUseParams)));
$result = $stmt->getResult();
}
}
else
{
// execute this.
$result = $mysqli->query($sQry);
}
return $result;
}
Doesn't look right, but I can't test that where I am. The problem with the bind is that it expects to bind via reference, and dynamic counts in MySQLi are not accommodated so you need to invoke a callable/closure/reflection type in order for it to process. But something does look a little off.
If that works as I intend it to, than it would ultimately return a mysqli_result regardless of if it used a statement or not. I can't be 100% sure it would work; I've used pretty much prepared statements exclusively since the mysqli was released.