Skip to content

Instantly share code, notes, and snippets.

@julianjupiter
Created January 22, 2018 02:03
Show Gist options
  • Save julianjupiter/dae9c8d185eb5614b4720a4fdcc31e95 to your computer and use it in GitHub Desktop.
Save julianjupiter/dae9c8d185eb5614b4720a4fdcc31e95 to your computer and use it in GitHub Desktop.
<?php
function sql($terms)
{
$q = "SELECT * FROM table1 WHERE ";
$i = 0;
foreach ($terms as $each)
{
$i++;
if ($i == 1)
{
$q .= "name LIKE '%$each%' ";
}
else
{
$q .= "OR name LIKE '%$each%'";
}
}
return $q;
}
$connection = mysqli_connect("localhost", "root", "admin", "jtest");
$search = isset($_GET['search']) ? $_GET['search'] : '';
if (empty($search))
{
echo 'Walang laman si get';
}
else
{
$terms = explode(' ', $search);
$q = sql($terms);
$query = mysqli_query($connection, $q);
$count = mysqli_num_rows($query);
if ($count > 0)
{
while ($row = mysqli_fetch_assoc($query))
{
echo $row["name"] . "<br>";
}
}
else
{
echo "No result";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment