-1

This query works, but does not display a 'no results' message. Should it be echo instead of exit on line 6?

<?php

    $key = $_GET['key'];
    $array = array();
    $con = mysql_connect("localhost","xxx","xxx");
    $db = mysql_select_db("xxx",$con);

    $query = mysql_query("select * FROM byartist2columns WHERE byartistnd LIKE '%{$key}%'");
    if( mysql_num_rows($query) == 0 ) exit( "No results" );
    while($row=mysql_fetch_assoc($query))
    {
        $array[] = $row['byartist'];
    }
    echo json_encode($array);

?>

JAVASCRIPT

<script> 
    $(document).ready(function() {
      $('input.typeahead').typeahead({
        name: 'typeahead',
        remote: 'r-search.php?key=%QUERY',
        limit: 150
      });
    });
</script>
Sachin PATIL
  • 745
  • 1
  • 9
  • 16
  • 2
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Mar 26 '17 at 17:47
  • What is $run?? I think it should be `mysql_num_rows($query)` – Yolo Mar 26 '17 at 17:51
  • Thanks Yolo. That stopped it breaking, but it is not displaying the no results message – user3317371 Mar 26 '17 at 17:54
  • Try to be more specific: are you trying queries that you know for sure they should return 0 results? Then, what do you see? Any error messages? A blank screen? – gmc Mar 26 '17 at 18:41
  • yes. I'm deliberately searching for things that are not there and I would like the result to say 'no results'. Currently, it returns a blank results set – user3317371 Mar 26 '17 at 18:46
  • Try this in code `if( mysql_num_rows($query) == 0 ) { $array[] = "no result"; echo json_encode($array); exit; }` – Deep 3015 Mar 26 '17 at 19:02

1 Answers1

0

Change condition as below.

if( mysql_num_rows($query) == 0 ) { $array[] = "No results"; }
Sachin PATIL
  • 745
  • 1
  • 9
  • 16