php - Only read array if SQL search gives results
**SOLVED ** Sorry everyone, there was something wrong in the database causing these issues.
I am trying to read info from the database. And when there are rows as a result I would like to do something. My current code is like this:
$sql="SELECT state FROM table_name WHERE finished=0 AND start=1";
$result = $conn->query($sql);
var_dump($result);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
The problem I am having now that even when there is no row with finished=0, the array still gives some rows and results. The output of the var_dump will be something like this:
object(mysqli_result)#5 (5) { ["current_field"]=> int(0) ["field_count"]=> int(3) ["lengths"]=> NULL ["num_rows"]=> int(2) ["type"]=> int(0) }
If I analyze it right this is seen as rows and the code is still executed. Is there an approach to prevent this? Or would you deal with this issue in a complete different way?
Your help is much appreciated.
Answer
Solution:
you need a FROM in your query like: