搜索栏查找用户

时间:2015-09-05 00:19:27

标签: php mysql search

打开新主题因为已经取得了进展,现在需要更多的帮助。

我创建了find.php:

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

另一页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.";


   ?>

现在,我正在我的网站上尝试它,它告诉我没有结果。 我正在尝试在mysql表中找到所有用户名如fullname。

如果找到,则回显用户个人资料图片30x30,并将其姓名链接到他的个人资料页面。

但它不起作用。希望得到一些帮助。

2 个答案:

答案 0 :(得分:1)

只需更改此行即可解决问题。

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

答案 1 :(得分:0)

  

首先,您必须从mysql更改为mysqli

更改此部分代码

//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%'";

   }  

并且你必须显示sql错误才能知道什么是真正的hapen

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

在这里

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