2

Found a youtube tutorial from 2012 that makes a basic search form, the code below renders the form fine, connects to the db (no error thrown) but when searching doesn't output any information. I'm using php 5.5 on my computer with XAMP for the server.

My guess is that some of the code is depreciated, I changed the mysql to mysqli, but this hasn't worked. Can anyone spot what might be wrong? I've wasted an hour or so already...

<?php

error_reporting(E_ALL);

require_once ("settings.php");

$conn = mysqli_connect($host, $user, $pswd)
    or die('Failed to connect to server');

mysqli_select_db($conn, $dbnm)
or die('Database unavaliable');

$output = "";

//collect
// between '' has to be first field in form
if(isset($_POST['search'])) {
    $searchq = $_POST['search'];
    // replaces anything not first argument to second argument #i allows both capital and non
    //$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);



    $query = "SELECT * FROM members WHERE firstname LIKE '%" . $searchq . "%' OR lastname LIKE '%" . $searchq . "%'";
    $results = mysqli_query($conn, $query);
    // returns int of rows picked up
    $count = mysqli_num_rows($results);
    if($count == 0) {
        $output ='There was no search results!';
    } else {
        while($row = mysqli_fetch_array($results)) {
            $fname = $row['firstname'];
            $lname = $row['lastname'];
            $id = $row['id'];

            $output .= '<div>' .$fname. ' '.$lname.'</div>';

        }
    }
}


?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Search</title>
</head>
<body>

<form action="index.php" method="post">
    <!-- input type, the value of the input to be searched, a placeholder html5 element. -->
    <input type="text" name="search" placeholder="search for members...">
    <input type="submit" value=">>" />

</form>

<?php

print ("$output");

?>

</body>
</html>
Jcode
  • 195
  • 2
  • 12
  • did you just straight up copy this? maybe you don't have any table entries – Kevin Apr 19 '16 at 02:40
  • 5
    The first thing I'd do is remove the `@` from the calls to `mysqli_connect` and `mysqli_select_db`, because it suppresses error messages. I'd also add the statement `error_reporting(E_ALL);` at the top as a matter of course. – Darwin von Corax Apr 19 '16 at 02:40
  • 1
    Also, try viewing the source of the generated page - there might actually be a message there that you are not seeing – Paul Coldrey Apr 19 '16 at 02:45
  • I copied some, I have a table in my database, I also made the settings.php file I'm not completely new to PHP, but wanted to attempt using a tutorial for incorporating ajax, but first needed this simple form – Jcode Apr 19 '16 at 02:52
  • Also made the changes with the @ and added error_reporting, the code still doesn't find any search results. I'm searching for entries I know are in the table. – Jcode Apr 19 '16 at 02:55
  • Would it matter they are stored as a varchar? – Jcode Apr 19 '16 at 02:58
  • No, that's the normal way to store short strings in a database. – Barmar Apr 19 '16 at 03:07
  • Are you getting the message "There was no search results"? Or nothing at all? – Barmar Apr 19 '16 at 03:09
  • There's nothing deprecated here. The old `mysql_XXX` functions are deprecated, but the replacement `mysqli_XXX` are OK. – Barmar Apr 19 '16 at 03:10
  • have you tried adding this to your code, right after mysqli_query? `$e = mysqli_error($conn); if($e) die($e);` – Dominick Navarro Apr 19 '16 at 03:11
  • I get nothing at all, which leads me to believe its a syntax problem. – Jcode Apr 19 '16 at 05:38
  • A syntax problem will always result in an error. Thus you have to believe in something else – Your Common Sense Apr 19 '16 at 06:21
  • First try testing if the query works manually, echo the query after you inserted the variables and execute that directly on the database. or add an error report after executing your query see http://php.net/manual/en/mysqli.error.php – Jester Apr 19 '16 at 06:53

0 Answers0