Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

php - How to select first and last data row from a mysql result?

SELECT * from User returns 75 users. Is it possible to select 1st user, and 75th user without doing while($row = mysql_fetch_assoc($result)) ?? and how?

UPDATE

Just to be more clear: I need to have SELECT * as I need the first and 75th user before I do while mysql_fetch_assoc so ASC, DESC, LIMIT answers not required.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
SELECT * from User LIMIT 1
UNION
SELECT * from User LIMIT 74,1

Edit

@Kay: PHP can't change the internal order of the resultset after it's created.

If the query always returns 75 rows then the only way to access the 1st and the 75th before anything else would be to use mysql_data_seek which moves the internal result pointer:

$result = mysql_query('SELECT * from User');

mysql_data_seek($result, 1);
$row1 = mysql_fetch_assoc($result);

mysql_data_seek($result, 75);
$row75 = mysql_fetch_assoc($result);

Note that if the above is followed by a while, the pointer must be reset to a suitable position.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...