Question

I am trying to create a search bar on my website to search for users name. When using mysql on my database the code works fine but when I use it on my website the results are not the same or don't work at all. Not sure if there is a setting I'am missing. Any help would be great.

In mysql database this works.

SELECT * 
  FROM `users` 
 WHERE username LIKE '%neva%'

This returns the user named Nevada.

On my site this doesn't work.

$friendquery = "neva"

if(isset($_GET['friendquery']){
    $friendquery = preg_replace("/[^0-9a-zA-Z ]/", "", $_GET['friendquery']);
    $sql = "SELECT * FROM users WHERE username LIKE '%$friendquery%'";

    $query = mysqli_query($db_connect, $sql);
    $u = $row["username"];
    echo $u;
}

This returns nothing. Yes I'am connected to the database.

The weird part is that some names work and some don't. Like I said any help would be great.

Was it helpful?

Solution

You have error in your php condition, so control is not going to SQL at all

here is error :

(isset($_GET['friendquery'])

u need extra closing braces => )

So condition should be :

if(isset($_GET['friendquery']))
     /* notice extra closing ^^ braces */

In addition

$friendquery = "neva"

if(isset($_GET['friendquery']))

^^ this condition is bound to fail always if you are not fetching friendquery from url....so, if u want to execute it in relations to $friendquery = "neva", then, use

if($friendquery != "")

And finally :

$row = mysqli_fetch_array($query)

is also missing from your code.

So final code should be :

if(isset($_GET['friendquery'])){ /* close extra brackets*/
    $friendquery = preg_replace("/[^0-9a-zA-Z ]/", "", $_GET['friendquery']);
    $sql = "SELECT * FROM users WHERE username LIKE '%$friendquery%'";

    $query = mysqli_query($db_connect, $sql);
    $row = mysqli_fetch_array($query) /* fetch array */
    $u = $row["username"];
    echo $u;
}

OTHER TIPS

Your code has two problems:

  1. Minor typo, major implications: you're missing a parenthesis ()) in the if statement

  2. Your mysqli_query results need to be fetched before it can be used. We use a loop which is run for each of the matching rows.

So your PHP would be:

$friendquery = "neva"

if(isset($_GET['friendquery'])){ // typo: missing a closing parenthesis
    $friendquery = preg_replace("/[^0-9a-zA-Z ]/", "", $_GET['friendquery']);
    $sql = "SELECT * FROM users WHERE username LIKE '%$friendquery%'";

    $query = mysqli_query($db_connect, $sql); // run the mysql query
    while($row = mysqli_fetch_array($query)){ // actually get each query results and 
                                              // put them in $row
    $u = $row["username"]; // use them!
    echo $u;
}

I think you missed the $row. Try this code

if(isset($_GET['friendquery'])){
    $friendquery = preg_replace("/[^0-9a-zA-Z ]/", "", $_GET['friendquery']);
    $sql = "SELECT * FROM users WHERE username LIKE '%$friendquery%'";

    $query = mysqli_query($db_connect, $sql);
    if(mysqli_num_rows($query) > 0)
    {
       while($row =   mysqli_fetch_array($query))
       {
         $u = $row["username"];
         echo $u;
       }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top