SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    odd numbers only - PHP 4

    Hello,

    I am trying to do one of two things.

    I need to pull only the id's with an odd number value from my MySQL db table but I need some help.

    I have this query to pull even numbers that works beautifully:

    PHP Code:
    $query "select id from table where id % 2"
    Is there a way to reverse this and pull anything !% 2? because putting the "NOT" in the query doesnt work.

    The other thing I know I can do with PHP 5 is the
    PHP Code:
     range(11002); 
    function but the third parameter doesn't work in PHP4.


    Does anyone have any suggestions?

    Thanks in advance.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    where not id % 2

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher View Post
    where not id % 2
    ahh I just transposed. Thanks. it's late here...

    EDIT: Hmm its not returning any results.

  4. #4
    SitePoint Addict AfroNinja's Avatar
    Join Date
    Oct 2006
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To be honest I'm not sure why your even query works at all, because you didn't finish the statement. Unless there's something about mysql I don't know... I mean that's like saying select ID from TABLE where 4+3

    ..4+3 what?

    anyway it should be
    SELECT * FROM `table` WHERE id%2 !=0
    The Flash Gaming Network
    Editorial reviews for the latest flash games!
    Afro Ninja Productions
    Original flash games and content from a guy with an afro

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    or, you could query the database fully, then filter out the evens after like:
    PHP Code:
    $q mysql_query("SELECT * FROM `table`");
    $oddarray = array();
    $i=0;
    while(
    $row=mysql_fetch_array($q)){
    if(
    $row['id'] % 2!=0){
    $oddarray[$i] = array();
    $oddarray[$i][0] = $row['id'];
    $oddarray[$i][1] = $row['name'];
    $oddarray[$i][2] = $row['description'];
    $oddarray[$i][3] = $row['body'];
    $i++
    }}; 
    which will make a multidimensional array.
    $oddarray holds an array for each row found, in that array is the data you may want.
    Last edited by Jake Arkinstall; Dec 17, 2006 at 04:13. Reason: !!!
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    note: for some reason, in the PHP tag, a % sign is replaced by "%". just change the "%" to a "%" and it'll do.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    None of these seem to work. I already have the evens pulling and it works fine. if I add quotes around the % sign it just reads the ! as the value 0.

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    where not id % 2

    works fine for me, although i tested with mysql 5.

    i noticed something. you said that this
    where id % 2

    returns EVEN numbers. it should return odd numbers.

    an even number % 2 will always be 0.
    an odd number % 2 will always be 1.

    1 is true, 0 is false. this is why

    select foo from bar where 1

    will return all records.

  9. #9
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $query = "select id from table where id % 2 = 1";


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •