Search code examples
selectmysqliwhere-clausesql-like

Read multiple entries with a Where clause


I tried to build a User search function but i ran into a problem a while ago. My function, which looks like this:

"Select id, username FROM users WHERE username LIKE '%search%'"

Is showing me only the user Janamaris ( userlist is a bit lower ) but as you see jns.drws has also a j in his username ( J = $search ).

So my Problem now i want to show all users with a J in his/her username

So how can i get all users with WHERE?

My Signed Users are [id, username]

  1. Tolkosino

47, Janamaris

48, TheCrazyMan

49, jns.drws


Solution

  • Beispiel einer Foreach Schleife bzw. While Schleife Posten?

    English: Can someone post an example of a foreach e.g. while loop?

    The sql is fine and will get all rows you need:

    "Select id, username FROM users WHERE username LIKE '%search%'"

    If you have problems with upper/lower case then read this: https://stackoverflow.com/a/2876820/4916265

    Example with while

    if($result = $mysqli->query($sql)){
     if(is_object($result)){
       while($row = $result->fetch_assoc()){
         echo "<div id=\"content-b\">";
         echo "<p class=\"usrsrch\">User ID : " . $row['id'] . "";
         echo "<br>";
         echo "<p class=\"usrsrch\">Username : . $row['username'] . "";
         echo "</div>";
       }
      } else {
        echo 'nix gefunden';
      }
    }