Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  140] [ 1]  / answers: 1 / hits: 58934  / 11 Years ago, sat, february 1, 2014, 12:00:00

I want to select all even posts id's from a table in my MySQL db and then display them. I also want to get all posts with odd id's and display them somewhere else.



I want to do this with PHP since that is the server-side language I am using.



Alternatively, would I have to select all posts and then check if they are even/odd with JavaScript? I would prefer PHP, but if it works with JavaScript that would be fine too.



Example of what I want:



table:



==================================================
id | text
==================================================
==================================================
| 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
==================================================
| 2 | turpis quis aliquet commodo, urna quam viverra justo, in |
==================================================
| 3 | Etiam in lectus sem. Nullam molestie nisl vel nunc consectetur |
==================================================
| 4 | Vestibulum eu molestie sapien. Ut luctus nulla vel libero sagittis |
==================================================


my failed attempt at a table in stackoverflow ^^



I want to display the even ones first, and the odd ones second:



Even Rows:






turpis quis aliquet commodo, urna quam viverra justo, in



Vestibulum eu molestie sapien. Ut luctus nulla vel libero sagittis






Odd Rows:



Lorem ipsum dolor sit amet, consectetur adipiscing elit



Etiam in lectus sem. Nullam molestie nisl vel nunc consectetur


More From » php

 Answers
15

To select even or odd IDs from a MySQL table, you would use the modulo operator (like in PHP):



SELECT * FROM table WHERE (id % 2) = 0; # even
SELECT * FROM table WHERE (id % 2) > 0; # odd

[#72794] Friday, January 31, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rylee

Total Points: 658
Total Questions: 114
Total Answers: 116

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
;