Jump to content

PHP MYSQL SEARCH


Solar

Recommended Posts

One of the search tutorials listed on phpfreaks homepage left me with an error.

 

Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\searchuser.php on line 46

 

I read that this error could lead into missing quotation marks, brackets, and semi-colons.

 

Highlighted in red is line 46. Highlighted in yellow is what I added but still same error on line 46.

 

 

// 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) < 3) {

      $error[] = "Search terms must be longer than 3 characters.";

  }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 id, username, gender FROM users WHERE ";

     

      // grab the search types.

      $types = array();

      $types[] = isset($_GET['id'])?"`id` LIKE '%{$searchTermDB}%'":'';

      $types[] = isset($_GET['username'])?"`username` LIKE '%{$searchTermDB}%'":'';

      $types[] = isset($_GET['gender'])?"`gender` LIKE '%{$searchTermDB}%'":'';

     

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

     

      if (count($types) < 1)

        $types[] = "`gender` 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 `id`"; // order by title.

 

      $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[] = "{$i}: {$row['id']}<br />{$row['username']}<br />{$row['gender']}<br /><br />";

            $i++;

        }

      }

  }

}

 

function removeEmpty($var) {

  return (!empty($var));

}

 

IN PHP MODE:

<?php 
// 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) < 3) {
      $error[] = "Search terms must be longer than 3 characters.";
   }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 id, username, gender FROM users WHERE ";
      
      // grab the search types.
      $types = array();
      $types[] = isset($_GET['id'])?"`id` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['username'])?"`username` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['gender'])?"`gender` LIKE '%{$searchTermDB}%'":'';
      
      $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
      
      if (count($types) < 1)
         $types[] = "`gender` 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 `id`"; // order by title.

      $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[] = "{$i}: {$row['id']}<br />{$row['username']}<br />{$row['gender']}<br /><br />";
            $i++;
         }
      }
   }
}

function removeEmpty($var) {
   return (!empty($var)); 
} ?>

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/197946-php-mysql-search/
Share on other sites

<?php
$dbHost = 'localhost'; // localhost will be used in most cases
// set these to your mysql database username and password.
$dbUser = 'root'; 
$dbPass = 'passwordhere';
$dbDatabase = 'login'; // the database you put the table into.
$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());

mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());
?>
<html>
   <title>My Simple Search Form</title>
   <style type="text/css">
      #error {
         color: red;
      }
   </style>
      <?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>
      <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">
         Search For: <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /><br />
         Search In:<br />
         Body: <input type="checkbox" name="id" value="on" <?php echo isset($_GET['id'])?"checked":''; ?> /> | 
         Title: <input type="checkbox" name="username" value="on" <?php echo isset($_GET['username'])?"checked":''; ?> /> | 
         Description: <input type="checkbox" name="gender" value="on" <?php echo isset($_GET['gender'])?"checked":''; ?> /><br />
                 Match All Selected Fields? <input type="checkbox" name="matchall" value="on" <?php echo isset($_GET['matchall'])?"checked":''; ?><br /><br />
         <input type="submit" name="submit" value="Search!" />
      </form>
      <?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?>
<?php
// 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) < 3) {
      $error[] = "Search terms must be longer than 3 characters.";
   }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 id, username, gender FROM users WHERE ";
      
      // grab the search types.
      $types = array();
      $types[] = isset($_GET['id'])?"`id` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['username'])?"`username` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['gender'])?"`gender` LIKE '%{$searchTermDB}%'":'';
      
      $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
      
      if (count($types) < 1)
         $types[] = "`gender` 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 `id`"; // order by title.

      $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[] = "{$i}: {$row['id']}<br />{$row['username']}<br />{$row['gender']}<br /><br />";
            $i++;
         }
      }
   }
}

function removeEmpty($var) {
   return (!empty($var)); 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/197946-php-mysql-search/#findComment-1038694
Share on other sites

The error is here:

if (isset($_GET['search'])) {
   $searchTerms = trim($_GET['search']);
   $searchTerms = strip_tags($searchTerms); // remove any html/javascript.
  }

 

Change it to:

if (isset($_GET['search'])) {
   $searchTerms = trim($_GET['search']);
   $searchTerms = strip_tags($searchTerms); // remove any html/javascript.
  // this brace was an extra which caused an error // }

Link to comment
https://forums.phpfreaks.com/topic/197946-php-mysql-search/#findComment-1038697
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.