Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2003
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to query text field from mysql

    I have the following code in one of my php files to pull from mysql database:
    PHP Code:
    $result mysql_query("SELECT DATE_FORMAT(date, '%m-%d-%y') AS formattedDate , track, horse, price FROM claimers where track_code =1 ORDER BY date DESC"); 
    It works fine, when the database field is an integer.

    The problem I'm having is when the database field is a CHAR field. I don't know how to use the where clause?

    Thanks.

    jls

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if the track_code field was a CHAR field then you would need to wrap the value in single quotes, like this:

    PHP Code:
    $result mysql_query("SELECT DATE_FORMAT(date, '%m-%d-%y') AS formattedDate , track, horse, price FROM claimers where track_code = '1' ORDER BY date DESC"); 
    Should work.

  • #3
    Regular Coder
    Join Date
    Jun 2003
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, I tried it, but it didn't work. I got this error message: (note I changed the field name from above example)

    Parse error: parse error, unexpected T_STRING

    Here's my line of php code:
    PHP Code:
    $result = @mysql_query('SELECT horse, farm, state, earnings, age, time, fee FROM 2006_stallion_fees where gait= 'P' ORDER BY fee DESC limit 35'); 
    The database field "gait" is a char(1)

    Thanks again.

  • #4
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, sorry, my mistake, i forgot to escape the quatation marks. Either wrap the entire query in double quatation marks and leave the single ones as they are, like this:

    PHP Code:
    $result = @mysql_query("SELECT horse, farm, state, earnings, age, time, fee FROM 2006_stallion_fees where gait= 'P' ORDER BY fee DESC limit 35"); 
    Or leave the query wrapped in single quatation marks and escape the ones within the string, like this:

    PHP Code:
    $result = @mysql_query('SELECT horse, farm, state, earnings, age, time, fee FROM 2006_stallion_fees where gait= \'P\' ORDER BY fee DESC limit 35'); 
    I personally prefer the first method.

  • #5
    Regular Coder
    Join Date
    Jun 2003
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yep, that was it. Thanks for helping me.

    jls


  •  

    Posting Permissions

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