Forum Moderators: coopster
$last_result = $result_array = array();
// First check if the query returned rows
if( mysql_num_rows($result) ) {
while( $result_array[] = mysql_fetch_array($result ) );
array_pop($result_array);
// Get last row
$last_entry = $result_array[count($result_array)-1];
}
if( !empty($last_entry) ) {
// Last row in $last_entry
} else {
// Handle error
}
i want to build a link to my last addition in my database
SELECT id_product FROM product WHERE id_category = '2' AND active = '1' ORDER BY insert_timestamp DESC LIMIT 1
With mysql_fetch_array you can just put the last record in a variable if i'm right.
$query = "SELECT id_product FROM product WHERE id_category = '2' AND active = '1' order by id_product asc";
$result = mysql_query($query);
$data = array();
while($row = mysql_fetch_array($result))
{
$i=1;//start the count
$count = mysql_num_rows($result);//count the rows
foreach($row as $id)
{
$data['row_'.$i] = $id['id_product'];//insert each record in the $data array
$i++;//move to the next sequential number
}
}
//this would create an array such as
$data['row_1'] = '1234';//record 1
$data['row_2'] = '1235';//record 2
$data['row_3'] = '1236';//record 3
//to access the last record use the $count var. Say $count = 5:
echo 'The last product id in the system is: '.$data['row_'.$count];
//to show all the records:
$new_i = 1;
while($count > 1)
{
echo 'Here is a list of all the prod id\'s in the system: '.$data['row_'.$new_i].'<br />';
$new_i++;
}
But there must be a way to put every record from an while loop in an array.