I've got a field in my MySQL DB which has a user ID or their IP.
I want to select the last 5 rows which has it's own ID or IP
so if it was
5 66 5 44 411 1111 411 1 5 77
My code will find
5 66 44 411 1111 1
how can i do this?
Hi, if you want to select only 5 from table then try something like LIMIT 0,5 It would be like this: $sql = "SELECT * FROM table"; $sql .= " LIMIT 5";
but if you don't want to repeat for instance 5 then you can add GROUP BY UserID
NZ_Kiwis wrote:I want to select the last 5 rows which has it's own ID or IP
There is no such thing as the first or last row in SQL. It is totally unsorted. You can of course sort it when using the select if you have a counter or a datetime field, but then you use that column to sort.
Also, since you have tried already it would be good to know how your database looks like and what query you use now.
okay this is my sql line,
$result3 = mysql_query ("SELECT COUNT(*) From user_usage where timestamp > $timec order by record_id limit 5"); $row3 = mysql_fetch_array($result3); $replies = $row3[0];
but I don't want it to count the same user_id twice
Rather than counting, get the ids from the database store them in an array and use array_unique on the array. You can then count the number of elements in the array.
eh???
$result3 = mysql_query ("SELECT record_id From user_usage where timestamp > $timec order by record_id limit 5"); while ($row3 = mysql_fetch_array($result3)) { $replies[] = $row3; } $replies = array_unique($replies);
$replies will then contain the unique id numbers which you can echo or count
comes up with Array
when I echo $replies
What is wrong?
You echo the elements of $replies e.g. $replies[0] etc.