1

I'm trying to get the very last table data row. I have this code:

// Connects to your Database 

 mysql_connect("*", "*", "*") or die(mysql_error()); 
 mysql_select_db("*") or die(mysql_error()); 


 // Collects data from table 
 $data = mysql_query("SELECT * FROM ps_product_lang JOIN ps_image ON ps_image.id_product = ps_product_lang.id_product WHERE id_lang = '1' ORDER BY ps_product_lang.id_product DESC LIMIT 16") 
 or die(mysql_error());

// puts the info into the $info array 
 $info = mysql_fetch_array( $data );




define('COLS', 6); // number of columns
$col = 0; // number of the last column filled

echo '<tr>'; // start first row

while($info = mysql_fetch_array( $data )) 
{ $col++;
  if ($col == COLS) // have filled the last row
  { $col = 1;
    echo '</tr><tr>'; // start a new one
  }

 echo '<td width="150" height="150" align="center" class="newprod">'; 
 Print  (strtolower (str_replace(" ","-","<a/href='/shop/".$info['id_product'] . "-".$info['link_rewrite'] . ".html'><img/src='http://wearitnewear.com/shop/".$info['id_image'] . "-medium/".$info['link_rewrite'] . ".jpg'></a>")));
 echo '<br/>';
 Print " ".$info['name'] . " "; 
 echo '</td>';
 } 


echo '</tr>'; // end last row

I have set the limit to 16 but it only pulls 15 entries from the table; the very very last one it will not pull out and display.

So if I say have

1
2
3
4
5

it will only show

1
2
3
4

but my limit will have to be set to 5

4
  • 1
    The problem is probably in the way that you are looping over the recordset in your application code.
    – eggyal
    Oct 21, 2012 at 21:25
  • so how would i fix it? i cant find anything on the net that has what im looking for Oct 21, 2012 at 21:30
  • Sounds like an 'off by one error' in your php code. For example when you are counting in PHP are you starting your count at zero or one? Either way can be made to work but you have to be consistent.
    – AllInOne
    Oct 21, 2012 at 21:34
  • "how would i fix it"? Try editing your question to include your code snippet that loops over the resultset, then we might stand some chance of identifying where the problem is! :)
    – eggyal
    Oct 21, 2012 at 21:36

1 Answer 1

0
// Connects to your Database 


mysql_connect("*", "*", "*") or die(mysql_error()); 
mysql_select_db("*") or die(mysql_error()); 

// Collects data from table 
$data = mysql_query("SELECT * FROM ps_product_lang JOIN ps_image ON ps_image.id_product = ps_product_lang.id_product WHERE id_lang = '1' ORDER BY ps_product_lang.id_product DESC LIMIT 16") or die(mysql_error());

if ( $data ) {
    echo '<tr>'; // start a new one
    $column = -1 ;
    $column_count = 6 ;
    while($info = mysql_fetch_array( $data )) 
    {
        $column++ ;
        if ( $column == $column_count ) {
            echo '</tr><tr>' ;
            $column = 0 ;
        }
        echo '<td width="150" height="150" align="center" class="newprod">'; 
        Print  (strtolower (str_replace(" ","-","<a/href='/shop/".$info['id_product'] . "-".$info['link_rewrite'] . ".html'><img/src='http://wearitnewear.com/shop/".$info['id_image'] . "-medium/".$info['link_rewrite'] . ".jpg'></a>")));
        echo '<br/>';
        Print " ".$info['name'] . " "; 
        echo '</td>';
    }
    echo '</tr>' ;
}
mysql_free_result($data);
4
  • ok that works..now i just need to get my columns back..it displays all in a straight line down the center of the page. i need 5 columns or a way to edit columns to my liking Oct 21, 2012 at 23:29
  • Hi @Tad Just changed the answer to consider the column widths. Oct 21, 2012 at 23:36
  • nvm i fixed it with your help..thanks kjtl..it was just that one line i had extra in there throwing it off Oct 21, 2012 at 23:38
  • Cheers @TadYearley ... remember to check for a result being trying to use the result of a query and to release the resource when you are finished. Oct 21, 2012 at 23:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.