Question

I'm trying to create a searchengine, to search members but when I execute the searchquery it gives me the error above. The error is about the last line in the code

Also the results are not really right.. it gives me the first characters of every word in the columns

So when I typ in 'Jan' or 'Vandenbergh' (first name, family name) it gives me J (first character from Jan, V (Vandenbergh) and so it goes on. With every character it gives an error.

The result of the selectquery:

'SELECT * FROM tblusers WHERE name = 'jan' OR surname ='jan''

This is my function:

public function Search($searchinput)
    {
        $db = new Db();
        $select = "SELECT * FROM tblusers WHERE name = '" . $searchinput . "' OR surname ='" . $searchinput . "'";
        var_dump($select);
        $result = $db->conn->query($select);
        return $data=$result->fetch_assoc();

    }

This is what the action button does:

if(isset($_POST["btnSearch"])){
    try {
        $searchinput = $_POST['btnSearch'];
        $searchresult = $user->Search($searchinput);
    }
    catch(exception $e){
        $feedback = $e->getMessage("no results");
    }
}

HTML:

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
            <input type="text" name="btnSearch" placeholder="search"/>
            <input type="submit" value="search members" />

      </div>
    </div>

<div class="row">
    <?php
    foreach ($searchresult as $result) {            
echo "<div><p>" . $result['surname']  . "</p></div>"; } ?> </div>
Was it helpful?

Solution

PHP

public function Search($searchinput)
{
        $db = new Db();
        $select = "SELECT * FROM tblusers WHERE name = '" . $searchinput . "' OR surname ='" . $searchinput . "'";
        $result = $db->conn->query($select);
        return $result;
}

if(isset($_POST["btnSearch"])){
try {
    $searchinput = $_POST['btnSearch']; // remember to filter that variable since you can be easily attacked
    $searchresult = $user->Search($searchinput);
}
catch(exception $e){
    $feedback = $e->getMessage("no results");
}

}

HTML

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
            <input type="text" name="btnSearch" placeholder="search"/>
            <input type="submit" value="search members" />

      </div>
    </div>

<div class="row">
    <?php
        if(is_object($searchresult))
            while($row = $searchresult->fetch_array()) {            
                 echo "<div><p>" . $row['surname']  . "</p></div>"; 
            } 
    ?> 
</div>

OTHER TIPS

->fetch_assoc() only returns 1 row, not an array of rows, so $searchresult is a 1 level array, not an array of arrays, so you would have accessed it as $searchresult['surname']

Based on your comments that you want 1 or more results, in your function change

return $data=$result->fetch_assoc();

to ->fetch_all()

return $data=$result->fetch_all(); // you could also do ->fetch_all(MYSQLI_ASSOC)

Also, add an isset() around your results -

<div class="row">
    <?php
    if(isset($searchresult)){
       foreach ($searchresult as $result) {            
          echo "<div><p>" . $result['surname']  . "</p></div>"; 
       }
    } ?> 
</div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top