0

I'm trying to create a search function on my website. I've been following a few videos etc on how to get it working but thus far the only reply I'm getting from my search bar is no results found. Could you guys take a look, to see where I have gone wrong. Thanks

$output = '';

if (isset($_POST['search'])){
    $searchq = $_POST['search'];
    $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

    $query = mysqli_query("SELECT * FROM loanusers WHERE username LIKE '%$searchq%' OR userid LIKE '%$searchq%'");
    $count = mysqli_num_rows($query);

    if ($count == 0) {
        $output = 'No results found';
    }else{
        while($row = mysqli_fetch_array($query)){
            $username = $row['username'];
            $userid = $row['userid'];

            $output .= '<div> '.$username.' '.$userid.' </div>';
        }
    }


}


 <body>
<form action="allusers.php" method="post">

<input type="button" name="Create User" value="Create New User" onclick="window.location.href='adminadduser.php'">
<input type="text" name="search" placeholder="Search Users...">
<input type="submit" value=">>">

</form>

<?php print("$output");?>

Mucca019
  • 201
  • 1
  • 3
  • 16

1 Answers1

1

mysqli_query takes a link identifier as its first argument. Create a connection to your database using mysqli_connect (there are examples for how to do this in the linked documentation) and use that connection variable in your mysqli_query statement.

// create the link using appropriate values here
$link = new mysqli($host, $user, $password, $db);

// Then you can use the link to execute your query
$query = mysqli_query($link, "SELECT * FROM loanusers... etc");
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • thankyou, I did have a connection page but missed it out when typing the query. added this is and now workings, thanks for your help – Mucca019 May 26 '16 at 19:00