You posted 2 questions, and for me they are related. The answers however seem to be indirect as an answer goes, so maybe a brief discussion?
I like to use class properties and methods whenever possible (read: I understand them), not only for convenience sake, but also for stability and life-time sake. There is 1) a level of trust that it has been tested and 2) that it will remain at least until somebody thoroughly tests it. Additionally, should the property or method ever change, a Find...Replace usually clears it right up.
Past this, consider what I do:
(object)$db=new mysqli(HOST_DEF, USER_DEF, PASS_DEF, DB_DEF);
if($db->connect_errno || $db->errno) {
//Do error stuff here
}
(string)$sql = sprintf("SELECT * FROM Table WHERE User='%s'",
filter_input(INPUT_POST, '$UserStringPassedInMaybe'));
(object)$result=$db->query($sql);
if(!$result || $result->num_rows()===0) {
@$result->free();
$db->close();
unset($sql);
//Handle that stuff here
}
You want to trap (quantify) the recordset somehow, and you can kind of compact how you do it. However, if all you're wanting to do is test the presence of a recordset, and you do it enough, then you can save a few lines of code by farming it out to function that returns a boolean, but you're still evaluating that return, when you could just evaluate $result itself as a boolean (as long as it's empty). Kind of dirty since it either evaluates as a boolean false, or an array. In this sense, no, you do not need num_rows.