php - Only read array if SQL search gives results

850

**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.

992

Answer

Solution:

you need a FROM in your query like:

$sql="SELECT state FROM tablename WHERE finished=0";
$result = $conn->query($sql);
var_dump($result);
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {

People are also looking for solutions to the problem: php - imagegif() not uploading to site

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.