我正在尝试获取最后一个表数据行..我有这个代码:

时间:2012-10-21 21:22:36

标签: mysql limit

我正在尝试获取最后一个表数据行。我有这段代码:

// 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

我已将限制设置为16,但它只从表中提取了15个条目;最后一个它不会拉出来显示。

所以,如果我说有

1
2
3
4
5

它只会显示

1
2
3
4

但我的限制必须设置为5

1 个答案:

答案 0 :(得分: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);