0

我需要一点帮助。这段代码错过了第一行,我不知道原因。我在网上搜索了很多,但每个人都在谈论 mysql_fetch_array($results) 但我的代码中没有类似的东西。

你发现这段代码有什么问题吗?

<?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); 

?>
4

1 回答 1

3

请注意mysql_result的 PHP 页面上的此消息

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

这将使您的记录指针超过第一个结果,因此您对 mysql_fetch_array 的调用将关闭一个。

无论如何,我认为你所追求的是

<?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); 

?>
于 2013-06-13T18:13:14.330 回答