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>