if (!isset($_GET['search_term']))
		{

		echo "<p class='warning'>Please enter something to search for.</p>";

	}

	else
	{

		$search_term = $_GET['search_term'];

		// check for data

		if (($search_term == '' || $search_term == NULL) && !isset($_GET['page_number']))
		{

			echo "<p class='warning'>Please enter something to search for.</p>";

		}

		else
		{

			$search_term = htmlentities(($conn->real_escape_string($search_term)));

			// start the first query

			$query1 = "SELECT COUNT(*) FROM posts WHERE post_body LIKE '%$search_term%'";

			if ($result1 = $conn->query($query1))
			{

				// make sure something was returned

				if ($result1->num_rows < 1) // HOW THE HECK AM I SUPPOSED TO FIND OUT IF NOTHING WAS FOUND SINCE COUNT ALWAYS RETURNS ONE???
				{

					echo "<p class='warning'>Your search didn't match anything in our database.</p>";

				}

				else
				{

					// pull from the database

					$row = $result1->fetch_row();

					// assign the total to a variable

					$total_entries = $row[0];

					// close the first result and set the number of page entries

					$result1->close();

					$entries_per_page = 10;

					if (isset($_GET['page_number']))
					{

						// assign the page number to a variable

						$page_number = $_GET['page_number'];

					}

					else
					{

						// give it 1

						$page_number = 1;

					}

					// divide and round total_entries with entries_per_page

					$total_pages = ceil($total_entries / $entries_per_page);

					// set the number of pages to be displayed

					$first_page = max(1, $page_number - 9);

					$last_page = min($total_pages, $page_number + 6);

					// tell the database where to start fetching by setting an offset

					$offset = ($page_number - 1) * $entries_per_page;

					// get the entries out and display them

					$query2 = "SELECT posts.post_id, posts.author_id, posts.author_name, DATE_FORMAT(posts.date_posted, '%W, %M %d, %Y %l:%i %p') AS formatted_date, posts.author_email, posts.category_id, categories.category_name, posts.title, posts.post_body FROM posts INNER JOIN categories ON posts.category_id = categories.category_id WHERE posts.post_body LIKE ? ORDER BY post_id DESC LIMIT " . $offset . ", " . $entries_per_page;


					//$query2 = "SELECT posts.post_id, posts.author_id, posts.author_name, DATE_FORMAT(posts.date_posted, '%W, %M %d, %Y %l:%i %p') AS formatted_date, posts.author_email, posts.category_id, categories.category_name, posts.title, posts.post_body FROM posts WHERE posts.post_body LIKE ? INNER JOIN categories ON posts.category_id = categories.category_id ORDER BY post_id DESC LIMIT " . $offset . ", " . $entries_per_page;

					$result2 = $conn->prepare($query2);

					$result2->bind_param('s', $search_string);

					$search_string = str_pad($search_term, strlen($search_term) + 2, '%', STR_PAD_BOTH);

					$result2->execute();

					$result2->bind_result($post_id, $author_id, $author_name, $date_posted, $author_email, $category_id, $category_name, $post_title, $post_body);

					while ($result2->fetch())
					{

						// format the line breaks

						$post_body = str_replace("\n", "\n<br/>", $post_body);

						// convert BBCODE

						$post_body = BBCODE($post_body);

						// bold the search term

						$post_body = preg_replace("/(\b" . str_replace('%', '', $search_string) . "\b)/Ui", "<b>$1</b>", $post_body);

						// display the news

						echo "<div class='news'><a href='http://www.uhrebirth.com/show_news/$post_id'>" . strip_tags($post_title) . "</a></div>";
						echo "<div class='news'><b>Posted by:</b> <a href='mailto:$author_email'>$author_name</a> in <a href='http://www.uhrebirth.com/news_categories.php'>$category_name</a></div>";
						echo "<div class='news'><b>$date_posted</b></div>";
						echo "<div class='news_post'>" . strip_tags($post_body, '<br/><b><i><a>') . "</div>";

					}

					echo "<br/><br/><br/>";

					// now, display the pages

					for ($i = $first_page; $i <= $last_page; $i++)
					{

						if ($i == $page_number)
						{

							echo $i;

						}

						else
						{

							echo "<a href='http://www.uhrebirth.com/news_search_results/page/$i'>$i</a>";

						}

					}

					// close result

					$result2->close();

				}

			}

		}

	}

}

// close and commit

$conn->commit();
$conn->close();

Since COUNT(*) will always return one on $result1->num_rows, I can't figure out how I'm supposed to echo my error message. Can someone help me?

    Well, yes. It returns one record containing a count of the number of records that match the query. That is what you're after, right? You store it in $total_entries further down.

      No, I meant my error message, as in

      if ($result1->num_rows < 1)
      

      I use the first result and query to paginate the results. I would really like to echo out an error message if nothing was found, thus not trying to retrieve anything with the second query after that.

        I know, but think about it: if the search didn't turn up any results and if you didn't have that check, what would $total_entries end containing? Now ask yourself what you might be able to do with that information.

          Write a Reply...