while循环只打印表中的一行,其他行未在表中打印

时间:2014-03-07 08:46:29

标签: php html mysql

这里的问题是表中没有打印所有值。我从数据库中收到所有信息,表格中只打印了一行,下面打印了其他值。因为,当我搜索名称“Alex”时,我在db中有更多具有相同名称的值,我得到了所有这些值,但表中只打印了一个值。

<div id="search">
    <form method="get" action="search_display.php"  id="searchform">
    <h1> Search </h1>
    <input type="text" name="query" />
    <input type="submit" value="Traži" /> 
</div>

//search_display.php   file
<?php
     $query = $_GET['query'];
     $min_length = 3;
     if(strlen($query) >= $min_length){

         $sql_result = mysql_query("SELECT * 
                                    FROM clanovi 
                                    WHERE (`IME` LIKE '$query') 
                                    OR (`PREZIME` LIKE '$query ') 
                                    OR (`FIRMA` LIKE '$query')")  or die(mysql_error());

         if(mysql_num_rows($sql_result) > 0){
            echo "<table border='5'>
            <tr>
              <th>IME</th>
              <th>PREZIME</th>
              <th>FIRMA</th>
              <th>ADRESA</th>
              <th>TELEFON</th>
              <th>FAX</th>
              <th>MOBITEL</th>
              <th>EMAIL </th>
              <th>WEB_STRANICA </th>
              <th>GRAD </th>
              <th>KATEGORIJA </th>
              <th>PROIZVODI </th>
            </tr>";
            while($row = mysql_fetch_array($sql_result)){    
            {
               echo "<tr>";
               echo "<td>" . $row ['IME'] . "</td>"; 
               echo "<td>" . $row ['PREZIME'] . "</td>";
               echo "<td>" . $row ['FIRMA'] . "</td>";
               echo "<td>" . $row ['ADRESA'] . "</td>";
               echo "<td>" . $row ['TELEFON'] . "</td>";
               echo "<td>" . $row ['FAX'] . "</td>";
               echo "<td>" . $row ['MOBITEL'] . "</td>";
               echo "<td>" . $row ['EMAIL'] . "</td>";
               echo "<td>" . $row ['WEB_STRANICA'] . "</td>";
               echo "<td>" . $row ['GRAD'] . "</td>";
               echo "<td>" . $row ['KATEGORIJA'] . "</td>";
               echo "<td>" . $row ['PROIZVODI'] . "</td>";    
               echo "</tr>";
            }
          echo "</table>";
         }                 
       }
       else{ // if there is no matching rows do following
           echo "Vaš unos ne postoji u bazi podataka!";
       }
    }
    else{ // if query length is less than minimum
      echo "Minimalna dužina je  ".$min_length. "slova";
    }
?>

1 个答案:

答案 0 :(得分:5)

你的循环语法无效,你有两个开口大括号:

while($row = mysql_fetch_array($sql_result)){
{
^ that one is extra

删除其中一个{

,然后

如果您不将%LIKE一起使用,那就像使用=一样,只返回完全匹配。更新您的查询以添加通配符%

SELECT * FROM clanovi 
WHERE (`IME` LIKE '%Alex%')
OR (`PREZIME` LIKE '%Alex%')
OR (`FIRMA` LIKE '%Alex%')")