Search code examples
mysqllimit

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


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


Solution

  • // 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);