PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Here is my search code, I have tried to use mysq_num_rows to echo 'No records found' if 0 results are found .. but it seems to just display blank page when 0 results are found.
I also added an if !$results error, so assumming i change a field in the query that doesnt exist in the table, it should echo that error .. but it seems to just give a line error.
if (mysql_num_rows($result) == 0) { echo "<center>no records found</center>"; } if (mysql_num_rows($result) == 0) { echo "<center>no records found</center>"; }
but if you dont get a mysql_error and the no results is still not working try
$num = mysql_num_rows($result);
echo $num.'---the number of rows returned!';
//if that is 0 then the following should work...
if (!mysql_num_rows($result))
{
echo 'no results';
}
The double "if" was a paste error .. actual code isnt like that.
With the code posted above, when I visit the page I see:
86---the number of rows returned!"user_name" -- "email_address"
for each user in the database, which is good as thats what I want. After submiting the search with a string that doesnt exist in the table, all I get is the form showing on the screen, without "no results". If I search with a sting that exists in the table the results are echoed correctly.
ohhhhhhh becuase if there is nothing in the $_POST[search] then when you do this if ($_POST[search]) then it will return false and show the form again because you didnt put anything in the search text field. or is that not the problem?
When I view 'search.php' I see all the records along with the search form, If I submit the from with no value it shows all the records in the table which is good. If I submit the form with a value that exists in the table it displays the matching records which is good, When I submit the form with a value that doesnt exist in the table, it only shows the search form with no data. Which is where 'no results' should be echoed to the screen.
PS. i am not sure if i remember it correctly, but it would have error if you try to use msql_fetch_array with no result found. I am too lazy to test it so if you use the code above, and it works, then my memory is still somewhat good.
I have another question, I have added pagination to the script. The small problem im having is when I search for a record it displays the matches found on page 1, but also adds links to pages 2,3,4 etc even tho there was only one or 2 matches, if there are more matches than the "$max_results" once you go to page 2 the search results are lost. How can I limit it by $result or mysql_num_rows rather than by the total records in the database?
Then maybe once a search is performed have a link "display all records"
I have tried modifying the following line with heaps of different code, but havnt managed make it pagination records found via the sql query rather than paginating all the records in the table:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM users"),0);
How would that have to be using maybe the variable $result or maybe mysql_num_rows?