Multiple likes and a must


#1

Hi All, I have a problem when using multiple wheres
in the code below is a query, I need to return the value if it meets like name, like number or like group
It also must be user_access
When i run i get the results of the else but when searching (the first query) i get
mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given
on line if(mysqli_num_rows($result) > 0)
Any help is appreciated

$output = ‘’;
$user_name = “bob”;

if(isset($_POST[“query”]))
{
$search = mysqli_real_escape_string($connect, $_POST[“query”]);

$query = "
SELECT * FROM contacts WHERE user_access LIKE ‘%".$user_name."%’
WHERE name LIKE ‘%".$search."%’ OR number LIKE ‘%".$search."%’ OR member LIKE ‘%".$search."%’
";
}
else
{

$query = "
SELECT * FROM contacts WHERE user_access LIKE ‘%".$user_name."%’ ORDER BY name LIMIT 5
";
}
$result = mysqli_query($connect, $query);

if(mysqli_num_rows($result) > 0)
{
$output .= ’

'; while($row = mysqli_fetch_array($result)) { $output .= ' '; } echo $output; } else { echo 'Not Found' ; }
Name Number Group
'.$row["name"].' '.$row["number"].' '.$row["member"].'

#2

Hi there!

The boolean return of your mysqli_query is expected behaviour when your given query is incorrect. It will simply return “false”. In this case, the most likely problem is the double “WHERE” statement you’re doing in your SQL:

It should be something like:
SELECT * FROM contacts WHERE user_access LIKE ‘%".$user_name."%’ AND name LIKE ‘%".$search."%’ OR number LIKE ‘%".$search."%’ OR member LIKE ‘%".$search."%’

Forgive me for any typo’s, this is just from the top of my head.

Hopefully this answers your question. :slight_smile: