have a website search script that searches my database then displays the search result like this :

item name
item number
view details for this item

scripts works fine i have one issue i have to fix on it.
Currently when a user enters a search that is less than 2 characters it displays a error to user that tells them "Search terms must be longer than 2 characters"
I have been trying to do the same thing with if a user enters in certain words by themselfs ect ( white, wheat, rye, bread) the database is for bread products and these 4 searches will result in the whole database to come up in the search results

Here is the code that i am using


<?php


require_once ('config.inc.php'); // error handler file


require_once ('connection file'); // Connect to the database.
// Set up our error check and result check array
$error = array();
$results = array();

// First check if a form was submitted. 
// Since this is a search we will use $_GET
if (isset($_GET['search'])) {
   $searchTerms = trim($_GET['search']);
   $searchTerms = strip_tags($searchTerms); // remove any html/javascript.

   if (strlen($searchTerms) < 2) {
      $error[] = "Search terms must be longer than 2 characters.";
     if (strlen($searchTerms) = wheat, white, rye, bread) {
      $error[] = "Your search terms are to broad, please try your search again.";

   }else {
      $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
   }

   // If there are no errors, lets get the search going.
   if (count($error) < 1) {
      $searchSQL = "SELECT link, keywords FROM products WHERE ";

  // grab the search types.
  $types = array();
  $types[] = isset($_GET['keywords'])?"`keywords` LIKE '%{$searchTermDB}%'":'';

  $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)

  if (count($types) < 1)
     $types[] = "`keywords` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked

      $andOr = isset($_GET['matchall'])?'AND':'OR';
  $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `keywords`"; 

  $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");

  if (mysql_num_rows($searchResult) < 1) {
     $error[] = "The search term provided {$searchTerms} yielded no results.";
  }else {
     $results = array(); // the result array
     $i = 1;
     while ($row = mysql_fetch_assoc($searchResult)) {
        $results[] = " {$row['link']}<br />";

     }
  }
   }
}

function removeEmpty($var) {
   return (!empty($var)); 
}
?>
<html>
   <title>Search</title>

<body>
      <div align="center"><?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>
      </div>
      <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">
         <div align="center">What are you looking for:
  <input name="search" type="text" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" size="50" /><input type="submit" name="submit" value="Search" />
         </div>
</form>
      <div align="center"><?php echo (count($results) > 0)?"Your search: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?></div>
</body>
</html>

this is the lines i am having the issue with

 if (strlen($searchTerms) < 2) {
      $error[] = "Search terms must be longer than 2 characters.";  // this one works
     if (strlen($searchTerms) = wheat, white, rye, bread) {
      $error[] = "Your search terms are to broad, please try your search again."; // this one doesn't

   }else {
      $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
   }

    also have been searching and reading on how to add pagination to the search result but can't make heads or tails out of it can someone point me where i can go to learn how to do this?

      darkangel2001lv;10997827 wrote:
      if (strlen($searchTerms) = wheat, white, rye, bread) { 
      

      First off, = is the assignment operator. To assign something, you need something to assign it to. The code you have gives an error along the lines of "can't assign to function return value".

      This tells me you do not have log_errors (should always be used) / display_errors (never in production environment) turned on, or your error_reporting is set to anything other than E_ALL & E_STRICT. Using -1 also works since the error_reporting uses bit flags to decide which errors to report. -1 means all bits are set to 1, thus making both present and future error types be included.

      Also, you should set these values in php.ini, not runtime. Parse errors means the script will never run, so if php.ini doesn't tell php to show/log errors, your script will never get the chance to do so in some cases.

      And since the statement above contains other errors as well, there really is no point in my telling you about them when you will see them as soon as logging/displaying is set up properly and you get rid of the "assignment to return value" error.

      If you do not understand what an error message means, feel free to post again asking about it.

      When it comes to pagination, the question has been answered here several times. The process is the same wether you do it with files in a directory or entries in a database, and if you can do one you should be able to achieve the other. Still, there are answers for both iirc.

        johanafm;10997838 wrote:

        First off, = is the assignment operator. To assign something, you need something to assign it to. The code you have gives an error along the lines of "can't assign to function return value".

        This tells me you do not have log_errors (should always be used) / display_errors (never in production environment) turned on, or your error_reporting is set to anything other than E_ALL & E_STRICT. Using -1 also works since the error_reporting uses bit flags to decide which errors to report. -1 means all bits are set to 1, thus making both present and future error types be included.

        Also, you should set these values in php.ini, not runtime. Parse errors means the script will never run, so if php.ini doesn't tell php to show/log errors, your script will never get the chance to do so in some cases.

        And since the statement above contains other errors as well, there really is no point in my telling you about them when you will see them as soon as logging/displaying is set up properly and you get rid of the "assignment to return value" error.

        I didn't understand much of what you said i am still learning php and mysql , with that said i do have a error file here is the code but i dont get any errors

        <?php 
        
        // This script determines how errors are handled.
        
        // Flag variable for site status:
        $live = FALSE;
        
        // Error log email address:
        $email = 'my website email';
        
        // Create the error handler.
        function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {
        
        global $live, $email;
        
        // Build the error message.
        $message = "An error occurred in script '$e_file' on line $e_line: \n<br />$e_message\n<br />";
        
        // Add the date and time.
        $message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />";
        
        // Append $e_vars to the $message.
        $message .= "<pre>" . print_r ($e_vars, 1) . "</pre>\n<br />";
        
        if ($live) { // Don't show the specific error.
        
        	error_log ($message, 1, $email); // Send email.
        
        	// Only print an error message if the error isn't a notice.
        	if ($e_number != E_NOTICE) {
        		echo '<div id="Error">A system error occurred. We apologize for the inconvenience.</div><br />';
        	}
        
        } else { // Development (print the error).
        	echo '<div id="Error">' . $message . '</div><br />';
        }
        
        } // End of my_error_handler() definition.
        
        // Use my error handler.
        set_error_handler ('my_error_handler');
        ?>
        

        wouldn't the php.ini you are talking about be this file above? file name is config.inc.php
        if not what do i need to do to get started, to figure this out.
        willing to learn

          johanafm;10997838 wrote:

          E_ALL & E_STRICT

          Error above: That should obviously be: E_ALL | E_STRICT

          darkangel2001lv;10997839 wrote:
          <?php 
          // This script determines how errors are handled.
          // Create the error handler.
          function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {
          }
          set_error_handler ('my_error_handler');
          ?>
          

          This sets my_error_handler() as the error handler and since you didn't specify which errors it should handle, it will handle all errors it can. However, for errors that it can't handle (such as errors occuring before the error handler is set, the default error handler is still used.
          Your error handler may also decide to pass the error on to the default error handler by returning true.

          Thus, I'd recommend setting error_reporting to E_ALL | E_STRICT, log_errors = path/to/errlogfile and either turning on or turning off display_errors depending on if you are in a production or development environment. I'd skip the error_handler unless you want to send yourself emails. If you do, I'd just check for if email should be sent, then wether it should or not, return true and fallback on default error handling.

          johanafm;10997838 wrote:

          wouldn't the php.ini you are talking about be this file above? file name is config.inc.php

          No. The php.ini file is indeed named php.ini and handles PHP settings. The above file is PHP code which is executed after php.ini settings have been applied.

          To find out where php.ini is, this will work in both CLI and via the web (although you may get different results for them)

          phpinfo();
          

            Thank you for your help johanafm, was able to track error using your method and now works great. Couldn't have gotten without your help

              Write a Reply...