0

If I have 5 rows in my mysql database with ids of 2, 4, 5, 6 and 8 and I echo out id 5 how do I then echo out the row number which of course would be row 3? I get row 3 by counting all the rows from the start of the database to the row I am after.

3
  • It appears you already realise the solution might involve count. So what have you tried already? Aug 22, 2011 at 20:37
  • 1
    Remember that you can't count on this row number to be constant. SQL query without ORDER BY clause doesn't guarantee any order.
    – piotrp
    Aug 22, 2011 at 20:48
  • I've tried a block of code which echos all the rows I have in the database, and another piece of code which can echo out how many rows in my database.
    – Martin L
    Aug 22, 2011 at 20:50

3 Answers 3

1

Try to use

SELECT @row_num:=@row_num+1 as row_number, id from table inner join (select @row_num:=0) as temp

$pdo = new PDO(....);
$result = $pdo->query($sql)->fetchAll();
foreach($result as $row) {
    print_r($row);
}
3
  • +1 You can also use a UNION instead of a JOIN to initialize the variable - handy if you want to avoid the overhead of a join: SELECT NULL AS row_num, NULL AS id FROM dual WHERE (@row_num := 0) UNION ... The WHERE produces an Impossible WHERE, so it doesn't return any rows, but it does initialize the variable.
    – Mike
    Aug 22, 2011 at 21:04
  • Ok, I've been using php for about a year so I know quite a bit but never have I come across a query like this. How exactly are you suppose to echo it out?
    – Martin L
    Aug 22, 2011 at 21:06
  • @Martin L: @row_num is a user-defined variable. It's handy for producing dynamic values in a query. If you do SELECT @row_num:=@row_num+1 AS row_num, then row_num will be returned as one of the columns in your query - just like a real column.
    – Mike
    Aug 22, 2011 at 21:09
0

The correct solution would be to add some auto_increment key to your table, then each row would have assigned a sequential number. Otherwise you are not guaranteed to get the same number for each id because SQL query without ORDER BY clause is not guaranteed to have any order (i.e. ids may be returned in random order).

Otherwise, the solution given by Andrej L is the correct one.

2
  • I made the ID column to be auto_increment when I first created the database.
    – Martin L
    Aug 22, 2011 at 21:04
  • A better description on what you are trying to achieve would help. Currently I see no real reason to do the counting you need.
    – piotrp
    Aug 22, 2011 at 21:07
0

Couldn't you just count the number of rows before this in the sort order you're using?

COUNT(*) FROM table WHERE id < ?

In this case only rows with id 2 and 3 would count, so the result is 2. If you're using a 1-based index, add one to that and you have your answer.

1
  • I'll give this a shot and let you know what's happening with it.
    – Martin L
    Aug 22, 2011 at 21:09

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.