-1

I would like to add the text ‘no records, please try again’ to search the search that comes up with nothing. Please be aware that I am a total beginner at this, the code I have is as following:

<?php
if (!empty($_REQUEST['term'])) {
    $term = mysql_real_escape_string($_REQUEST['term']);     
    $sql = "SELECT * FROM search WHERE surname LIKE '%".$term."%' ORDER BY surname, fullname, conflictsort"; 
    $r_query = mysql_query($sql);

    while ($row = mysql_fetch_array($r_query)){
        $id = $row['id'];
        $initial = $row['initial'];
        $surname = $row['surname'];
        $fullname = $row['fullname'];
        $dob = $row['dob'];
        $born = $row['born'];
        $service = $row['service'];
        $enlisteddate = $row['enlisteddate'];
        $enlisted = $row['enlisted'];
        $number = $row['number'];
        $rank = $row['rank'];
        $batt = $row['batt'];
        $unit = $row['unit'];
        $section = $row['section'];
        $died = $row['died'];
        $death = $row['death'];
        $memorial = $row['memorial'];
        $conflict = $row['conflict'];
        $conflictsort = $row['conflictsort'];
        $biography = $row['biography'];
        $link = $row['link'];

        echo"<h4>$surname</h4>
        <b>Name:</b> $fullname - <b>Rank:</b> $rank ($number) - <b>Regiment:</b> $batt $unit $section
        <br /><b>Birth:</b> $dob $born
        <br /><b>Enlisted:</b> $service - $enlisteddate $enlisted
        <br /><b>Died:</b> $died $death
        <br /><b>Memorial:</b> Shown as $initial$surname at $memorial
        <br /><b>Confilct:</b> $conflict
        <br /><b><a href='$link'>$biography</a></b>
        <br /><br /><br />";
    }  
}

?>

1 Answers1

2

Use mysql_num_rows() function to check how many rows are returned from the SELECT query, and display results accordingly.

// your code
$r_query = mysql_query($sql);
if(mysql_num_rows($r_query)){
    while ($row = mysql_fetch_array($r_query)){
        // your code
    }
}else{
    echo "no records, please try again";
}

Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Thanks for your help Rajeep, I don't know where to put that code. Could you please repost all my code amended with the mysqli updates. – GrahamUK33 Jan 29 '17 at 00:24
  • 2
    @GrahamUK33 - He has literally already done that. You should _really_ be able to figure out how to implement this yourself. Just compare it with your own code. _Don't be lazy!_ – M. Eriksson Jan 29 '17 at 00:28
  • Its not a case of being lazy, I have tried putting the code in diffrent locations and can't get it to work. I'm sure if you know what you are doing with the code then it is a very easy job to do. – GrahamUK33 Jan 29 '17 at 00:33
  • @GrahamUK33 Since you're having difficultly in putting the code pieces together, here's the complete code, [http://pastebin.com/02tuei2u](http://pastebin.com/02tuei2u). However, you should go through the code and learn *how things work*, this will help you in writing your *own code* in future projects. – Rajdeep Paul Jan 29 '17 at 00:38
  • Thank you Rajdeep, I can see how some of the code is working, but its knowing where to place it all. – GrahamUK33 Jan 29 '17 at 00:52
  • I have tried to use the same code on another search php page I have and I can't get it to work. – GrahamUK33 Jan 29 '17 at 11:21
  • $title$description

    "; } } ?>
    – GrahamUK33 Jan 29 '17 at 11:21
  • @GrahamUK33 I don't see any `mysql_num_rows()` function being used in your code. See the code snippet given in my answer and align your code accordingly. Also, please don't post the same comment multiple times. – Rajdeep Paul Jan 29 '17 at 12:55
  • Thank you for your help again, after adding mysql_num_rows() to the code it worked. – GrahamUK33 Jan 29 '17 at 17:44