0

I use below php code to send results of MYSQL databse search to AJAX requests...

$query = $db->query("SELECT name FROM search WHERE qurl = '" . $queryString . "'");


if($query) {

    while ($result = $query ->fetch_object()) {             
           echo $result->name; 
    }
} else { echo 'no results found'; }

but I never get no results found message even there is no results, all I get if there is no result, two empty spaces - I found that using alert(data.length) in AJAX page, result was 2 which means php output has two empty spaces when there is no results...

but when there are results it works fine...

any way of removing these two spaces or why Im not getting no results found message?

1
  • I do hope $queryString is properly sanitized, because if it isn't, you're missing the whole point, and should be using prepared statements. Commented Nov 28, 2012 at 20:59

2 Answers 2

3
if ($query->num_rows > 0) {
  while ...
} else {
  echo 'no results found';
}

$db->query() only returns false if there was an error performing the query. An empty result set is not an error.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I tried it before but I get this error.. Fatal error: Call to undefined method mysqli_result::num_rows() in C:\wamp\www\7\load.php on line 18
Corrected. It's a property, not a method.
0

What you are evaluating is if the statement is valid... which it is, and will always return true. To evaluate the results returned, you'll need to evaluate $query->num_rows

2 Comments

I tried using num_row but I get a error.. error is Fatal error: Call to undefined method mysqli_result::num_rows() in C:\wamp\www\7\load.php on line 18
The answer above had parenthesis... make sure you don't have those. php.net/manual/en/mysqli-result.num-rows.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.