0

I need a little help. This code miss the first row, I don't know the reason. I've searched a lot around the web but everyone talk about the mysql_fetch_array($results) but there's nothing similar in my code.

Do you find something wrong in this code?

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

?>
3
  • SELECT * FROM products is your query.. how do you know what is supposed to be your first row when you don't use any ordering? Commented Jun 13, 2013 at 17:52
  • In the row where you assign $id, you reference $i. Where was $i assigned a value? Commented Jun 13, 2013 at 17:52
  • Where do you initialise $i? You initialise $id, but never re-set it when you're looping through your results, so it's only ever going to output the same value. (ditto for the other four variables) Commented Jun 13, 2013 at 17:53

1 Answer 1

3

Note this message on the PHP page for mysql_result

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

This is going to advance your record pointer past the first result so your calls to mysql_fetch_array are going to be off by one.

Anyway I think what you are after is

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

?>
Sign up to request clarification or add additional context in comments.

1 Comment

$id and $name should also be elements of $row: $row['id'] and $row['name'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.