当有更多时,Php / Mysql搜索只返回1个结果

时间:2012-11-09 06:31:52

标签: php mysql search

我在脚本中进行了双重搜索。当它搜索玩家装备时,它将获取玩家UID并带回该玩家的名字。唯一的问题是可能会返回多于1个结果,并且只会显示带有名称的最后一个UID。

$ip = "localhost";
$user = "******";
$pass = "*******";
$db = "hivemind";
$ill1 = $_POST['search'];

//Database Connection
$con = @mysql_connect("$ip:3316", "$user", "$pass")
            or die(mysql_error());

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

$sql = mysql_query("select PlayerUID, Inventory, Backpack from character_data where Inventory like '%$ill1%'");

while ($row = mysql_fetch_array($sql)) {
    $puid = $row['PlayerUID'];
    $inv = $row['Inventory'];
    $back = $row['Backpack'];
    ?>
    <html>
    <body>
    <table>
    <tr>

                    <td><?php echo "$puid"; ?></td>
                    <td><?php echo "$inv"; ?></td>
                    <td><?php echo "$back"; ?></td>

    </tr>
    </table>
    </body>
    </html>
    <?php }?>

    <?php

//Database Connection
$con = @mysql_connect("$ip:3316", "$user", "$pass")
            or die(mysql_error());

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

$sql = mysql_query("select PlayerUID, PlayerName from player_data where PlayerUID like '%$puid%'");

while ($row = mysql_fetch_array($sql)) {

    $puid2 = $row['PlayerUID'];
    $plnm = $row['PlayerName'];
    ?>
    <html>
    <body>
    <table>
    <tr>    
                    <td><?php echo "$puid"; ?></td>
                    <td><?php echo "$plnm"; ?></td>


    </tr>
    </table>
    </body>
    </html>
     }

2 个答案:

答案 0 :(得分:1)

它将返回所有结果。你没有正确打印它们。

将html,body,table标签移到while循环之外。

将循环更改为:

while ($row = mysql_fetch_array($sql)) {

    $puid2 = $row['PlayerUID'];
    $plnm = $row['PlayerName'];
    echo "<tr><td>".$puid."</td>";
    echo "<td>".$plnm."</td></tr>";
} 

答案 1 :(得分:0)

循环

中打印下面的代码
<td><?php echo "$puid"; ?></td>
  <td><?php echo "$plnm"; ?></td>

您只打印一次,因此会覆盖值。