I have a mysql query that I get data from mysql table. How can I properly check if there is result returned? My mysql table have 5 rows with data.
I have tried to equate mysqli_num_rows
to 0 but it does not work. It always return fail. I need it to return success.
I can't figure this out, how can I fix this?
my code:
$result=mysqli_num_rows($query)
if($result==0)
{
echo "Success";
}else
{
echo "fail";
}
Your statement only evaluates to true
if mysqli_num_rows
is equal to 1
:
if($test = mysqli_num_rows($result) == 0){
You should check if it is equal more than 0, so that it will evaluate true when it returns more than 1 row:
$result = mysqli_num_rows($query)
if ($result == 0) {
echo "Success";
} else {
echo "fail";
}
Usage of mysqli_num_rows()
:
Returns the number of rows in the result set.
you have an issue with this line
result==0 //also no $ sign here
//try like this
if ($result){
}
hope so it will help
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM Table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// data is available
} else {
echo "0 results";
}
$conn->close();
?>
for more please read this php_mysql_data
OR
$q='query here';
$result=mysqli_query($con,$q);
if (!$result)
echo(mysqli_error($con));
if(mysqli_num_rows($result)>0){
//result here
}