This question already has an answer here:
I know this question has been asked loads of times, and I have looked at the answers but I cant get my code to work correctly. The code I have tried is displayed below.
<?php
include 'connection.php';
$connection=mysqli_connect("localhost","root","","c3392262");
if (isset($_POST['search'])) {
$search = $_POST['search'];
if (!empty($search)) {
$query = "SELECT name FROM products WHERE name LIKE '%$search%'";
$query_run = mysqli_query($connection, $query);
if (mysql_num_rows($query_run)>=1) {
echo 'found';
} else {
echo 'No results found';
}
}
}
?>
<form name = "searchbar" method = "POST" action = "searchBar.php">
<input name = "search" type = "text" size = "40" maxlength = "50" >
<input type="submit" value="Submit">
</form>
</div>
You are mixing mysql_
and mysqli_
functions. Don't do that, they are incompatible. Stick with mysqli_
.
You mixed mysql
& mysqli
. Use mysqli_num_rows
instead
if (mysqli_num_rows($query_run) >= 1) {
echo 'found';
} else {
echo 'No results found';
}
mysqli_num_rows
and not mysql_num_rows
<?php
include 'connection.php';
$connection = mysqli_connect("localhost", "root", "", "c3392262");
if (isset($_POST['search'])) {
$search = $_POST['search'];
if (!empty($search)) {
$query = "SELECT name FROM products WHERE name LIKE ?";
$query_run = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($query_run , 's', '%'.$search.'%');
mysqli_stmt_execute($query_run );
if (mysqli_num_rows($query_run) > 0) {
echo 'found';
} else {
echo 'No results found';
}
}
}
?>