Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

asked on

mysqli search not working correctly returning multi found how do i search

im trying to search multiple fields and im getting multiple repeat returns
how do i properly make my code return correct info and the code does not limit the results too (is that due to finding multi matches for same records?)

how do i fix my code to return unique and can limit the returns please

also is prepare need here if so how would i implement it here.

thank you in advance for any code or help you may provide

my sql line (built)
sql: SELECT * FROM iwia_events WHERE (event_title LIKE '%food%' OR event_location LIKE '%food%' OR event_text LIKE '%food%' OR extra_info LIKE '%food%' OR categories LIKE '%food%' OR tags LIKE '%food%' OR type LIKE '%food%') AND event_start >= now() ORDER BY event_start ASC LIMIT 0,5
Searching for: food 

Open in new window


my code so far
<?php
include("../../mysqli_ctx.php");

if ($_POST['limit']) {
     $limit = " LIMIT ".$_POST['limit'];
}
elseif ($_GET['limit']) {
     $limit = " LIMIT ".$_GET['limit'];
}else {
	$limit = " LIMIT 0,15 \n";
}    
       
if ($_POST['search_value']) {
     $e = $_POST['search_value'];
}
elseif ($_GET['search_value']) {
     $e = $_GET['search_value'];
}
else {
    //die("no var set or passed");
    $e ="";
}  
$search_like = "";

if ($e) {
    $search_like = "WHERE (event_title LIKE '%$e%' OR \n" .
           "event_location LIKE '%$e%' OR \n" .
           "event_text LIKE '%$e%' OR \n" .
           "extra_info LIKE '%$e%' OR \n" .
           "categories LIKE '%$e%' OR \n" .
           "tags LIKE '%$e%' OR \n" .
           "type LIKE '%$e%') \n" .
           " AND event_start >= now() ORDER BY event_start ASC \n";
}

   $sql = "SELECT * FROM iwia_events " .$search_like."".$limit;

   echo "sql: ".$sql."<br>";
   //$query_result = $mysqli->query($query);
   $res = $mysqli->query($sql);
 $result_tb ="";  
// IF mysqli_query() RETURNS FALSE, LOG AND SHOW THE ERROR
if (!$res)
{
    $err
    = "QUERY FAIL: "
    . $sql
    . '<br> ERRNO: '
    . $mysqli->errno
    . '<br> ERROR: '
    . $mysqli->error
    ;
    trigger_error($err, E_USER_ERROR);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESULT OBJECT IN $res
// AND SO WE CAN NOW USE $res IN OTHER MYSQLI FUNCTIONS

// DETERMINE HOW MANY ROWS OF RESULTS WE GOT
$num     = $res->num_rows;
$num_fmt = number_format($num);
if (!$num)
{
    //echo "<br/>QUERY: $sql ";
    $result_tb .= "<br/>No Entrys Found<br> ";
    echo PHP_EOL;
}
else
{
        if ($e) {
            echo "Searching for: ".$e." "."<br>";
        } else {
            //
        }
         
            //echo "<br/>QUERY: $sql ";
    if ($num_fmt = 1) {
        //$result_tb .=" FOUND $num_fmt Event <br/><br/>";
        echo PHP_EOL;
    } else {
       //$result_tb .=" FOUND $num_fmt Events <br/><br/>";
       echo PHP_EOL; 
    }
    
    
}
$event = array();
// do event data output block
while ($row = $res->fetch_array())
{
    $event[] = $row;
}

?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Johnny

ASKER

@PortletPaul thank you

im definitely getting dupes as i have a id number in the link and im getting more then one of the same returned.. so far i cant track t down.
ok, but are you running the EXACT query that yo show in the question?

If not please post that query

"repetition" can be introduced to a query by joins to other tables for example.
Avatar of Johnny

ASKER

accepted as it answered if i had it wrong or not..thank you.

fyi it was a nested problem with how i outputted it for printing i didn't have the <li> inside the do while for the output.

thanks for looking at my code
:) no problem, I did only look at the sql, PHP's not really my thing.
Thanks for the grading, Cheers, Paul.