How can I add another error message if the query is empty? I tried with mysqli_num_rows
without success. :/
$que = new mysqli($host, $db_user, $db_password, $db_name);
$tbl = $que->query
("
SELECT iditem, nazwa
FROM `idlist2`
WHERE nazwa LIKE '%$name%'
");
while($value=$tbl->fetch_assoc())
{
if(strlen($name) == 0 || strlen($name) == NULL)
{
echo '<font color="green">!</font>';
break;
}
echo $value['iditem'] . $value['nazwa'] . "<br>";
}
Here's one way to do it:
while($value=$tbl->fetch_assoc())
{
$notempty = true; // set a variable if you fetch anything
if(strlen($name) == 0 || strlen($name) == NULL)
{
echo '<font color="green">!</font>';
break;
}
echo $value['iditem'] . $value['nazwa'] . "<br>";
}
if (!isset($notempty)) {
echo 'other error message'; // show the other message if it isn't set
}
You are actually doing this the wrong way. mysqli_num_rows
is used when you are querying in a procedural manner.
In your code sample, you created a new object by calling new mysqli()
. To get the number of rows fetched when using OOP construct, you should get the value of num_rows
property. And this is how to go about that:
if ($tbl->num_rows > 0) {
while($value=$tbl->fetch_assoc())
{
if(strlen($name) == 0 || strlen($name) == NULL)
{
echo '<font color="green">!</font>';
break;
}
echo $value['iditem'] . $value['nazwa'] . "<br>";
}
} else {
echo "No result found";
}
$que->close();