about search on mysql

Discussion in 'PHP' started by baris22, Feb 24, 2010.

  1. #1
    hello,

    I am trying to match a field on mysql. this is the query i use.
    
    $query = "SELECT * FROM orderr  WHERE orderr_collect_time like \"%$mynewdate%\" ORDER BY orderr_id DESC LIMIT $start, $limit";
    
    PHP:
    $mynewdate format is: 23-Feb-2010
    data format in mysql is: 23-Feb-2010 00:57:11

    I do not need to match the time. I only need to match the date. Is the query i am using is ok. for this purpose? Will i allways get the right data?

    Thanks
     
    baris22, Feb 24, 2010 IP
  2. Houdas

    Houdas Well-Known Member

    Messages:
    158
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #2
    What data type is the column "orderr_collect_time"? If it is DATETIME, you can't use the format 23-Feb-2010. Try 2010-02-23 instead.
     
    Houdas, Feb 24, 2010 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    it is not datetime, it is varchar(50).
     
    baris22, Feb 24, 2010 IP
  4. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I dont think your query will result exact searched date, like if u search for 2010, it will result all results having year 2010. Doesnt matter whats the date..
     
    Om ji Kesharwani, Feb 24, 2010 IP
  5. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    I will never do a search for the year. my search will be under 1 format which is like: 23-Feb-2010. but format of the column in data base is like 23-Feb-2010 00:57:11
     
    baris22, Feb 24, 2010 IP
  6. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #6
    Yes, as long you do not search to the hours/minutes/seconds, you'll get the right data although it's varchar column :)
     
    xrvel, Feb 24, 2010 IP
  7. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $query = "SELECT * FROM orderr WHERE orderr_collect_time like '$mynewdate%' ORDER BY orderr_id DESC LIMIT $start, $limit";
     
    Om ji Kesharwani, Feb 24, 2010 IP
  8. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #8
    what is the difference between
    \"%$mynewdate%\"
    and
    '$mynewdate%'
     
    baris22, Feb 24, 2010 IP
  9. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I am not sure but u cant use Escape \ character in query.
    U can use my query as it is.
    If it dont work there is some pob in your code...
     
    Om ji Kesharwani, Feb 24, 2010 IP
  10. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #10
    % is a kind of asterisk.

    For example you have these data
    1. Big John Guy
    2. John Doe
    3. Small John
    4. Small Smart John

    Query
    SELECT * FROM users WHERE name LIKE '%john%';
    Code (markup):
    Will return whole data (4 data).

    Query
    SELECT * FROM users WHERE name LIKE 'john%';
    Code (markup):
    Will return this data
    1. John Doe

    While this query
    SELECT * FROM users WHERE name LIKE '%john';
    Code (markup):
    Will return these data
    1. Small John
    2. Small Smart John

    But in your case, i guess your query is fine :D
     
    xrvel, Feb 24, 2010 IP
  11. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #11
    thanks for the info.

    in this case i should use:

    
    SELECT * FROM orderr  WHERE orderr_collect_time like '$mynewdate%'
    
    
    Code (markup):
    to match 23-Feb-2010
    in 23-Feb-2010 00:57:11


     
    baris22, Feb 24, 2010 IP
  12. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #12
    Although your query will match the correct data, but this is not the best choice to match dates. At the minimum use DATETIME (yyyy-mm-dd time) to store dates. VARCHAR is not the best choice for any data type. It should only be used if you are uncertain about the data length. CHAR is a better choice.
    Thanks :)
     
    JEET, Feb 24, 2010 IP