-1

Total number of people: I hope one of you can help with this: The script I use on my website www.zerious.dk under "Genealogy" - "Name Lookup - test". It is used to search for people in my MySql database with a specific name, you can search by either first or last name - or parts of it - and it works fine. But it would be nice if it was possible also to get to know how many people are found by a search. Now the question is whether it is possible to put into my script so I also have found the number of people with the given name?

This is the script code:

<?php
if(isset($_POST['submit']))
{
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length)
{
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results =
mysql_query("SELECT * FROM person_st WHERE (`efternavn` LIKE '%".$query."%') OR   (`fornavn` LIKE '%".$query."%')");

echo "<table border='0' width='500' align='center' cellpadding='1' cellspacing='1'>";

if(mysql_num_rows($raw_results) > 0)
{ while($results = mysql_fetch_array($raw_results))
{ echo "<tr><td width='125px'>".$results['efternavn']."/td>td>".$results['fornavn']."  </td></tr>" ; }}

else{ echo "<tr><td colspan='2' height='20px'>Din søgning gav intet resultat! </td></tr>";
echo "</table>"; }}
else{ echo "Nej, den går ikke, du kan ikke søge på ingenting - skriv mindst  $min_length bogstav!"; }}
?>

1 Answers1

1

Just save the mysql_num_rows() value into a variable for reusability

$total_results = mysql_num_rows($raw_results);

and the use it for checking or displaying the result:

if($total_results) > 0)
{
     echo "Total found: " . $total_results;
     // the rest of your code
}

UPDATE: Just replace your if statement:

 if(mysql_num_rows($raw_results) > 0)

with the above lines so the script looks like:

$total_results = mysql_num_rows($raw_results);
if($total_results > 0)
{
     echo "Total found: " . $total_results;
Kypros
  • 2,997
  • 5
  • 21
  • 27
  • I am NOT a prof programmer so it will be a great help if you can show me where to put the code - please. – user3355305 Oct 05 '14 at 14:25
  • I comes up with this error:Parse error: syntax error, unexpected '>' – user3355305 Oct 05 '14 at 14:34
  • Typo in `if($total_results) > 0)` remove the first `)` closing parenthesis after $total_results so it is `if($total_results > 0)`. Updated the answer so you can copy/paste that but i suggest reading up for PHP/MySQL since you are making changes to a production site that could lead to problems!! – Kypros Oct 05 '14 at 14:37
  • Good, please accept the answer if it fixes your problem. – Kypros Oct 05 '14 at 14:48