AlphaOneIntelligence Posted June 21, 2023 Share Posted June 21, 2023 (edited) I only want the one record to come back but it brings back evert single record within the database. Please help, thanks Form To Fill In To Send It to the Database: <center><h1>SEARCH ANIMALS RECORDS</h1> <form action="animals-search.php" method="post"> <input type="text" name="search_query"><br> <br> <input type="submit" value="Submit"> </form></center> PHP: // Collect the search query from a form input $search = $_POST['search']; // Construct an SQL query to search for data in the MySQL database $sql = "SELECT id, animal_type, animal_breed, colour, owner_name, address, telephone, mobile, email, offence, offence_date, offence_location, case_status, case_ref, action_required, action_taken, microchipped, microchip_number, aggresive, dangerous, lost, date_lost, location_lost, stolen, date_stolen, location_stolen, found, date_found, location_found, other_information * FROM animals WHERE 1"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // Output the search results while($row = mysqli_fetch_assoc($result)) { echo "<br> " . $row["br"] . "<br>"; echo "Animal Type: " . $row["animal_type"] . "<br>"; echo "Animal Breed: " . $row["animal_breed"] . "<br>"; echo "Colour: " . $row["colour"] . "<br>"; echo "Owner Name: " . $row["owner_name"] . "<br>"; echo "Address: " . $row["address"] . "<br>"; echo "Telephone: " . $row["telephone"] . "<br>"; echo "Mobile: " . $row["mobile"] . "<br>"; echo "Email: " . $row["email"] . "<br>"; echo "Offence: " . $row["offence"] . "<br>"; echo "Offence Date: " . $row["offence_date"] . "<br>"; echo "Offence Location: " . $row["offence_location"] . "<br>"; echo "Case Status: " . $row["case_status"] . "<br>"; echo "Case Ref: " . $row["case_ref"] . "<br>"; echo "Action Required: " . $row["action_required"] . "<br>"; echo "Action Taken: " . $row["action_taken"] . "<br>"; echo "Microchipped: " . $row["microchipped"] . "<br>"; echo "Miccrochip Number: " . $row["microchip_number"] . "<br>"; echo "Aggressive: " . $row["aggressive"] . "<br>"; echo "Dangerous: " . $row["dangerous"] . "<br>"; echo "Lost: " . $row["lost"] . "<br>"; echo "Date Lost: " . $row["date_lost"] . "<br>"; echo "Location Lost: " . $row["location_lost"] . "<br>"; echo "Stolen: " . $row["stolen"] . "<br>"; echo "Date Stolen: " . $row["date_stolen"] . "<br>"; echo "Location Stolen: " . $row["location_stolen"] . "<br>"; echo "Found: " . $row["found"] . "<br>"; echo "Date Found: " . $row["date_found"] . "<br>"; echo "Location Found: " . $row["location_found"] . "<br>"; echo "Other Information: " . $row["other_information"] . "<br>"; echo "<br> " . $row["br"] . "<br>"; echo "<hr> " . $row["hr"] . "<br><br>"; } } else { echo "No Results Found"; } // Close the database connection mysqli_close($conn); ?> Edited June 21, 2023 by AlphaOneIntelligence Quote Link to comment https://forums.phpfreaks.com/topic/316992-php-brings-back-all-the-results-but-i-only-want-one-record-when-typed-in-query/ Share on other sites More sharing options...
requinix Posted June 21, 2023 Share Posted June 21, 2023 Looks like you might have confused WHERE with LIMIT. 1 Quote Link to comment https://forums.phpfreaks.com/topic/316992-php-brings-back-all-the-results-but-i-only-want-one-record-when-typed-in-query/#findComment-1609884 Share on other sites More sharing options...
ginerjm Posted June 21, 2023 Share Posted June 21, 2023 The purpose of the where clause is for you to specify which record you want. 1 Quote Link to comment https://forums.phpfreaks.com/topic/316992-php-brings-back-all-the-results-but-i-only-want-one-record-when-typed-in-query/#findComment-1609885 Share on other sites More sharing options...
ginerjm Posted June 21, 2023 Share Posted June 21, 2023 Or as requinix said, you could limit the number of records returned to 1 if that is what you want and don't care which record it is. Quote Link to comment https://forums.phpfreaks.com/topic/316992-php-brings-back-all-the-results-but-i-only-want-one-record-when-typed-in-query/#findComment-1609886 Share on other sites More sharing options...
Barand Posted June 21, 2023 Share Posted June 21, 2023 28 minutes ago, ginerjm said: The purpose of the where clause is for you to specify which record you want. He has. A query selects all records for which the WHERE expression evaluates to "true". As "1" is true for all records, they have specified that all records are selected. Quote Link to comment https://forums.phpfreaks.com/topic/316992-php-brings-back-all-the-results-but-i-only-want-one-record-when-typed-in-query/#findComment-1609887 Share on other sites More sharing options...
Strider64 Posted June 22, 2023 Share Posted June 22, 2023 (edited) You can do a full text content search on a `content` field, but the main thing is you have to search a column in order to find what you are looking for. This is done in PDO, but gives an example on what I'm trying to say - // Extract the search term and heading from the request, if they exist. $searchTerm = isset($request['searchTerm']) ? $request['searchTerm'] : null; $heading = isset($request['heading']) ? $request['heading'] : null; // If a search term was provided, use a full-text search on the 'content' field. // Before this can work, you'll need to make sure your content column is indexed for full-text searching. // You can do this with the following SQL command: // Example: // ALTER TABLE gallery ADD FULLTEXT(content); if($searchTerm !== null) { $sql = "SELECT * FROM gallery WHERE MATCH(content) AGAINST(:searchTerm IN NATURAL LANGUAGE MODE) LIMIT 1"; $stmt = $pdo->prepare($sql); $stmt->bindValue(':searchTerm', $searchTerm); // If a heading was provided, search for exact matches on the 'heading' field. } else if($heading !== null) { $sql = "SELECT * FROM gallery WHERE heading = :heading LIMIT 1"; $stmt = $pdo->prepare($sql); $stmt->bindValue(':heading', $heading); // If neither a search term nor a heading was provided, throw an exception. } else { throw new Exception("No valid search term or heading provided"); } // Execute the prepared statement. $stmt->execute(); // Fetch the results and handle them as needed. $results = $stmt->fetchAll(PDO::FETCH_ASSOC); Edited June 22, 2023 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/316992-php-brings-back-all-the-results-but-i-only-want-one-record-when-typed-in-query/#findComment-1609889 Share on other sites More sharing options...
gizmola Posted June 22, 2023 Share Posted June 22, 2023 It does appear this is intended to be a search so the where criteria needs to be implemented. Towards that goal, this is not going to work: <input type="text" name="search_query"><br> Followed by: $search = $_POST['search']; See the issue? (search_query is not search). This query is also not going to work as written: ...., date_stolen, location_stolen, found, date_found, location_found, other_information * Typically either enumerate the columns or use the wildcard '*', but certainly this syntax of 'other_information *' is invalid, as you would at very least need 'other_information, *'. Quote Link to comment https://forums.phpfreaks.com/topic/316992-php-brings-back-all-the-results-but-i-only-want-one-record-when-typed-in-query/#findComment-1609890 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.