engima Posted June 23, 2010 Share Posted June 23, 2010 hi guys, i'n new here and to php, so i followed the tutorial on this site about a search script. I used the following code: <?php $dbHost = 'localhost'; $dbUser = 'username'; $dbPass = 'password'; $dbDatabase = 'test'; $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()); mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error()); // 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 'A A A', 'B B', 'C', 'DD' FROM table WHERE "; // grab the search types. $types = array(); $types[] = isset($_GET['A A A'])?"`A A A` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['B B'])?"`B B` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['C'])?"`C` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['D D'])?"`D D` LIKE '%{$searchTermDB}%'":''; $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked) if (count($types) < 1) $types[] = "`Service ID` 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 `C`"; // 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['A A A A']}<br />{$row['B B']}<br />{$row['C']}<br />{$row['D D']}<br /><br />"; $i++; } } } } function removeEmpty($var) { return (!empty($var)); } ?> <html> <title>My Simple Search Form</title> <style type="text/css"> #error { color: red; } </style> <body> <?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 /> A A A: <input type="checkbox" name="A A A" value="on" <?php echo isset($_GET['A A A'])?"checked":''; ?> /> | B B: <input type="checkbox" name="B B" value="on" <?php echo isset($_GET['B B'])?"checked":''; ?> /> | C: <input type="checkbox" name="C" value="on" <?php echo isset($_GET['C'])?"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):""; ?> </body> </html> when i use the script the output just show's: 1: A A A B B C D D 2: A A A B B C D D But not the contence of the table, just the name of the search sting/value. can anyone help me, or point me in the right direction on what i'm doing wrong? cheers! enigma aka nathan Link to comment https://forums.phpfreaks.com/topic/205629-seach-phpmysql-blank-output/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.