Solar Posted April 8, 2010 Share Posted April 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197946-php-mysql-search/ Share on other sites More sharing options...
premiso Posted April 8, 2010 Share Posted April 8, 2010 The error is not in the code you posted. That could just be where it thought it was. Post your full script inside of the php tags if you want assistance to fix it. Quote Link to comment https://forums.phpfreaks.com/topic/197946-php-mysql-search/#findComment-1038692 Share on other sites More sharing options...
Solar Posted April 8, 2010 Author Share Posted April 8, 2010 <?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)); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197946-php-mysql-search/#findComment-1038694 Share on other sites More sharing options...
premiso Posted April 8, 2010 Share Posted April 8, 2010 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 // } Quote Link to comment https://forums.phpfreaks.com/topic/197946-php-mysql-search/#findComment-1038697 Share on other sites More sharing options...
Solar Posted April 8, 2010 Author Share Posted April 8, 2010 Thankyou for your reply. That Bracket was never there until I added to try it out and I kept it in the script sorry. But there is still the same error.. This one is a tough one. I can't even see it. Quote Link to comment https://forums.phpfreaks.com/topic/197946-php-mysql-search/#findComment-1038701 Share on other sites More sharing options...
premiso Posted April 8, 2010 Share Posted April 8, 2010 I am at a loss, copying what you had but removing that extra } I did a php -l file.php check on it and it returned no Syntax errors. Not sure what else I can do to test it, but if I cannot see the error I am hard put to fixing it. Quote Link to comment https://forums.phpfreaks.com/topic/197946-php-mysql-search/#findComment-1038704 Share on other sites More sharing options...
Solar Posted April 8, 2010 Author Share Posted April 8, 2010 My fault, I made a mistake in the database wise. I've re-edit the code to my likings and it works like a charm. Thanks for your help and time. Quote Link to comment https://forums.phpfreaks.com/topic/197946-php-mysql-search/#findComment-1038737 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.