用php显示多个搜索结果

时间:2012-11-04 21:04:38

标签: php mysql search

目前我的搜索脚本可以运行,但它没有按照我的意愿去做。目前它只返回一个搜索结果,即使搜索项目应该显示大约20个搜索结果。我试图弄清楚如何让脚本返回应该显示的缺失结果。所以也许这里的专家可以提供帮助:)

include('data.php');
    //Database Connection
$con=@mysql_connect("$ip", "$user", "$pass")
            or die(mysql_error());

//Select Database
$dbcon=@mysql_select_db($db, $con)
            or die(mysql_error());


$search = $_POST['search'];

$sql = mysql_query("select guid, name, staffmember, dateposted, id, admin_note from $whitelist where guid like '%$search%' or name like '%$search%' or staffmember like '%$search%' or dateposted like '%$search%' or id like '%$search%' or admin_note like '%$search%'");


while ($row = mysql_fetch_array($sql)) {
    //Username
    $name = $row['name'];
    //GUID
    $guid = $row['guid'];
    //Staff Member Who Submitted
    $staff = $row['staffmember'];
    //ID
    $id = $row['id'];
    //Date Posted
    $date = $row['dateposted'];
    //Note From Admin
    $admin = $row['admin_note'];
    }
    ?>
    <html>
    <body>
    <table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
                <tr>
                    <!--<th class="table-header-check"><a id="toggle-all" ></a> </th>-->
                    <th class="table-header-repeat line-left minwidth-1"><a href="">ID</a>  </th>
                    <th class="table-header-repeat line-left minwidth-1"><a href="">GUID</a></th>
                    <th class="table-header-repeat line-left"><a href="">Username</a></th>
                    <th class="table-header-repeat line-left"><a href="">Admin Note</a></th>
                    <th class="table-header-repeat line-left"><a href="">Staff Member</a></th>
                    <th class="table-header-repeat line-left"><a href="">Date Posted</a></th>
                    <th class="table-header-options line-left"><a href="">Options</a></th>

                </tr>
                <tr>

                    <td><?php echo "$id"; ?></td>
                    <td><?php echo "$guid"; ?></td>
                    <td><?php echo "$name"; ?></td>
                    <td><?php echo "$admin"; ?></td>
                    <td><?php echo "$staff"; ?></td>
                    <td><?php echo "$date"; ?></td>
                    <td class="options-width">
                    <?php
                 echo '<a href="edit.php?id=' . $row['id'] . '" title="Edit" class="icon-1 info-tooltip"></a>';
                echo '<a href="delete.php?id=' . $row['id'] . '" title="Edit" class="icon-2 info-tooltip"></a>'; 
                ?>

                    </td>
                </tr>

                </table>
                </body>
                </html>

2 个答案:

答案 0 :(得分:1)

while ($row = mysql_fetch_array($sql)) {
    //Username
    $name = $row['name'];
    //GUID
    $guid = $row['guid'];
    //Staff Member Who Submitted
    $staff = $row['staffmember'];
    //ID
    $id = $row['id'];
    //Date Posted
    $date = $row['dateposted'];
    //Note From Admin
    $admin = $row['admin_note'];
    }

你在这做什么是错的。 在循环的每次迭代中,变量都被重新定义,它们的旧值被删除并被新值覆盖。

答案 1 :(得分:0)

您需要在“while($ row = mysql_fetch_array($ sql))”循环中输出数据。就像现在一样,你的旧值被覆盖了它所获取的每一个新行。

在这种情况下,您的代码必须像

一样结构化
 print out table header
 while ($row = mysql_fetch_array($sql)) {
    print out table row
 }
 print out what comes after table

include('data.php');
//Database Connection
$con=@mysql_connect("$ip", "$user", "$pass")
        or die(mysql_error());

//Select Database
$dbcon=@mysql_select_db($db, $con)
        or die(mysql_error());


$search = $_POST['search'];

$sql = mysql_query("select guid, name, staffmember, dateposted, id, admin_note from $whitelist where guid like '%$search%' or name like '%$search%' or staffmember like '%$search%' or dateposted like '%$search%' or id like '%$search%' or admin_note like '%$search%'");?>


<html>
<body>
<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
            <tr>
                <!--<th class="table-header-check"><a id="toggle-all" ></a> </th>-->
                <th class="table-header-repeat line-left minwidth-1"><a href="">ID</a>  </th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">GUID</a></th>
                <th class="table-header-repeat line-left"><a href="">Username</a></th>
                <th class="table-header-repeat line-left"><a href="">Admin Note</a></th>
                <th class="table-header-repeat line-left"><a href="">Staff Member</a></th>
                <th class="table-header-repeat line-left"><a href="">Date Posted</a></th>
                <th class="table-header-options line-left"><a href="">Options</a></th>

            </tr>


<?php
while ($row = mysql_fetch_array($sql)) {
//Username
$name = $row['name'];
//GUID
$guid = $row['guid'];
//Staff Member Who Submitted
$staff = $row['staffmember'];
//ID
$id = $row['id'];
//Date Posted
$date = $row['dateposted'];
//Note From Admin
$admin = $row['admin_note'];
?>
            <tr>

                <td><?php echo "$id"; ?></td>
                <td><?php echo "$guid"; ?></td>
                <td><?php echo "$name"; ?></td>
                <td><?php echo "$admin"; ?></td>
                <td><?php echo "$staff"; ?></td>
                <td><?php echo "$date"; ?></td>
                <td class="options-width">
                <?php
 }

             echo '<a href="edit.php?id=' . $row['id'] . '" title="Edit" class="icon-1 info-tooltip"></a>';
            echo '<a href="delete.php?id=' . $row['id'] . '" title="Edit" class="icon-2 info-tooltip"></a>'; 
            ?>

                </td>
            </tr>

            </table>
            </body>
            </html>

(为了制作好的和安全的代码,应该改变一些事情,但为了让你开始,这应该工作)