2014-01-20 3 views
0

데이터베이스에서 테이블을 렌더링하는 PHP를 작성 중입니다. 간단해야하지만 어떤 이유로 인해 여분의 셀이 렌더링되고 모든 셀이 비어 있습니다. 여기 내 코드는 다음과 같습니다.테이블이 비어있는 이유는 무엇입니까?

<?php 
    $db= new PDO("mysql:host=localhost;dbname=mydb", "user", "password"); 
    $query= $db->query("SELECT yarnId, yarnName, longYarnDescription, sale_price, cost, contents, onSale, yarnImage, activeFlag FROM yarn"); 
    $result= $query->fetchAll(); 
?> 
<table border="1"> 
    <tr> 
    <th>yarnId</th> 
    <th>yarnName</th> 
    <th>description</th> 
    <th>sale price</th> 
    <th>cost</th> 
    <th>contents</th> 
    <th>onSale</th> 
    <th>yarnImage</th> 
    <th>activeFlag</th> 
    <th>edit</th> 
    </tr> 
    <?php for($r=0; $r<count($result); $r++){?> 
    <tr> 
     <?php for($c=0; $c<count($result[0]); $c++){?> 
      <td><?php echo $result[r][c];?></td> 
     <?php }?> 
     <td><button name=edit>edit</button></td> 
    </tr> 
    <?php }?> 
</table> 

누가 비어 있고 왜 여분의 셀이 있는지 말해 줄 수 있다면 크게 감사하겠습니다.

+0

for()while() 루프를 사용하지 않음 로그인 정보가 정확했다,하지만 난 당신의 로그인 자격 증명을 제거했으며 경우에 대비 자리로 대체하는 경우 확인합니다. – Lee

+0

fectAll 뒤에 이것을 인쇄하십시오 var_dump ($ result) : –

+0

중첩 된 루프로 무엇을 수행하고 있다고 생각하십니까? 결과 집합에서 한 번에 한 줄씩 표시하는 대신 전체 결과 집합을 가져 오는 이유는 무엇입니까? 결과 집합의 크기에 따라 훨씬 많은 메모리가 필요할 수 있습니다. –

답변

0

다음 코드 대신

<table border="1"> 
    <tr> 
    <th>yarnId</th> 
    <th>yarnName</th> 
    <th>description</th> 
    <th>sale price</th> 
    <th>cost</th> 
    <th>contents</th> 
    <th>onSale</th> 
    <th>yarnImage</th> 
    <th>activeFlag</th> 
    <th>edit</th> 
    </tr> 
<?php 
while($row = $query->fetch()) { 
    echo "<tr>"; 
    for ($x=0;$x<= 8; $x++) { 
     echo "<td>" . $row[$x] . "</td>"; 
    } 
    echo "<td><button name=\"edit\">edit</button></td></tr>\n"; 
} 
?> 
</table> 
관련 문제