Aviv Day
Aviv Day

Reputation: 438

Search Bar To Find Users

Opening new topic because got progressed and now need a bit more help.

I have created find.php:

         <h5>Search: </h5>
        <form action="search.php" method='get'>
            <input type='text' name='fullname' size='25' /> 
            <input type='submit' value='Search' /> 
    </form>

And another page search.php

         <h5>Search: </h5>
        <form action="search.php" method='get'>
            <input type='text' name='fullname' value='<?php echo $_GET['fullname']; ?>' size='25' /> 
            <input type='submit' value='Search' /> 
    </form>
   <?php
   $fullname = $_GET['fullname'];
   $terms = explode(" ", $fullname);

   $query = "SELECT * FROM users WHERE ";

   foreach($terms as $each){
        $i++;

        if($i == 1)
            $query .= "fullname LIKE '%$each%'";
        else
            $query .= "OR fullname LIKE '%$each%'";

   }  

    $query = mysql_query($query);
    $numrows = mysql_num_rows($query);

        if($numrows > 0){

            while($row = mysql_fetch_assoc($query)){
                $id = $row['id'];
                $fullname = $row['id'];
                $profile_picture = $row['profile_picture'];

                echo "<img src='www.secrethashtags.com/uploads/profile_picture/$profile_picture' height='50' width='50'> <br><a href='www.secrethashtags.com/profile.php?id=$id'>Aviv Day </a></br>";
            }

        } else
            echo "No #Results.";


   ?>

Now, i'm trying it on my website and it tells me no results. i'm trying to find all users with fullname like fullname in mysql table.

and if it finds, echo the user profile picture 30x30 and his name linked to his profile page.

but it doesn't work. Hope for some help.

Upvotes: 0

Views: 252

Answers (2)

Amir Saadallah
Amir Saadallah

Reputation: 686

at first you have to change from mysql to mysqli


change this part of code

//define $i     
foreach($terms as $each){
            $i++; // increment at the end of each iteration  

        if($i == 1)
            $query .= "fullname LIKE '%$each%'";
        else
            $query .= "OR fullname LIKE '%$each%'";

   }  

and you have to display the sql error to know what realy hapen

    $result = mysql_query($query) or die(mysql_error());;
    $numrows = mysql_num_rows($result);

and here

$id = $row['id'];
$fullname = $row['fullname']; 

Upvotes: 0

Beroza Paul
Beroza Paul

Reputation: 2117

Just change this line and it will fix your issue.

$query .= " OR fullname LIKE '%$each%'";

Upvotes: 1

Related Questions