2013-06-13 4 views
0

약간의 도움이 필요합니다. 이 코드는 첫 번째 행을 놓치고 그 이유를 모르겠습니다. 웹에서 많은 것을 검색했지만 모든 사람들이 mysql_fetch_array ($ results)에 대해 이야기하지만 내 코드에는 아무 것도 없습니다.첫 번째 행이 누락 된 PHP 및 mysql

이 코드에서 문제가 있습니까?

<?php 

// create query 
$query = "SELECT * FROM products"; 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

$id = mysql_result($result,$i,"id"); 
$name = mysql_result($result,$i,"name"); 
$imageurl = mysql_result($result,$i,"imageurl"); 
$price = mysql_result($result,$i,"price"); 
$quantity = mysql_result($result,$i,"quantity"); 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
    // yes 
    // print them one after another 
    echo "<table class='table table-hover'>"; 
    echo "<thead> 
       <tr> 
        <th>ID</th> 
        <th>Name</th> 
        <th>Image</th> 
        <th>Price</th> 
        <th>Quantity</th> 
       </tr> 
       </thead>"; 
    while($row = mysql_fetch_array($result)) { 
     echo "<tr>"; 
     echo "<td>".$id."</td><td>".$name."</td><td><a href='".$imageurl."' class='fancybox fancybox-effects-e' title='".$name."'><img src='".$imageurl."' alt='".$name."'></a></td><td>€ ".$price."</td><td>".$quantity."</td>"; 
     echo "</tr>"; 
    } 
    echo "</thead>"; 
    echo "</table>"; 
} 
else { 
    // no 
    // print status message 
    echo "No rows found!"; 
} 

// free result set memory 
mysql_free_result($result); 

// close connection 
mysql_close($connection); 

?> 
+0

무슨 생각? – Nikola

+0

'$ id'를 할당 한 행에서'$ i'를 참조합니다. '$ i'는 어디에서 값을 할당 받았습니까? –

+0

'$ i'는 어디에서 초기화합니까? '$ id'를 초기화하지만 결과를 반복 할 때 결코 다시 설정하지 않으므로 같은 값을 출력 할 것입니다. (다른 네 개의 변수에도 마찬가지 임) – andrewsi

답변

3

참고이 통화 한으로 해제 될 예정는 mysql_fetch_array 할 수 있도록 첫 번째 결과 과거 귀하의 기록 포인터를 전진하는 것입니다 mysql_result

Calls to mysql_result() should not be mixed with calls to other functions that deal with the result set. 

에 대한 PHP 페이지에이 메시지가.

어쨌든 내가 어떻게 당신이 어떤 순서를 사용하지 않는 경우 첫 번째 행이 있어야하는데 무엇을 알 수 있습니까 .. products` 당신의 쿼리입니다 FROM`SELECT * 당신은 이후

<?php 

// create query 
$query = "SELECT * FROM products"; 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
    // yes 
    // print them one after another 
    echo "<table class='table table-hover'>"; 
    echo "<thead> 
       <tr> 
        <th>ID</th> 
        <th>Name</th> 
        <th>Image</th> 
        <th>Price</th> 
        <th>Quantity</th> 
       </tr> 
       </thead>"; 
    while($row = mysql_fetch_array($result)) { 
     echo "<tr>"; 
     echo "<td>".$row["id"]."</td><td>".$row["name"]."</td><td><a href='".$row["imageurl"]."' class='fancybox fancybox-effects-e' title='".$row["name"]."'><img src='".$row["imageurl"]."' alt='".$row["name"]."'></a></td><td>€ ".$row["price"]."</td><td>".$row["quantity"]."</td>"; 
     echo "</tr>"; 
    } 
    echo "</thead>"; 
    echo "</table>"; 
} 
else { 
    // no 
    // print status message 
    echo "No rows found!"; 
} 

// free result set memory 
mysql_free_result($result); 

// close connection 
mysql_close($connection); 

?> 
+0

'$ id'와'$ name'은'$ row'의 요소가되어야합니다 :'$ row [ 'id']'와'$ row [ 'name']'. –

+0

@ NiilsWerner 감사합니다. – Orangepill

관련 문제