Hi Everyone, i am trying to retrieve search results from the database. I want to select name between the two dates (date from and Date to).

I have the following statement but im not sure of the syntax, Please

$search_query = ("SELECT * FROM `polls` WHERE `name` LIKE \"%$trimmed_search%"\ 
BETWEEN \"%$trimmed_dateF%"\ AND \"%$trimmed_dateT%"\ 
ORDER BY \"%$trimmed_dateF%"\); 

I keep getting this Error


Parse error: parse error in C:\wamp\www\poll_management\list.php on line 117

Thank you for your help

    you have several of these:
    \"%$trimmed_search%"\
    when it should be:
    \"%$trimmed_search%\"

    Also you have one quote ("SELECT .......); before select
    but you have no matching at the end of your string (".....");

      You've got a few problems...

      1. Your backslashes come after the closing double quote. If you're escaping double quotes because you used double quotes as a string delimiter, the backslash must precede the double quote.

        Speaking more generally, a backslash is used as an escape character (or as part of a special escape sequence, e.g. "\n") and is placed directly before the character it is meant to escape.

      2. You could simplify the escaping issue and simply use single quotes for the strings in the SQL query instead. In fact, if the ANSI_QUOTES SQL mode was enabled, using double quotes would actually cause errors (since they would be used for identifiers, e.g. database/table/etc. names).

      3. Why do you have percent signs (%) in the BETWEEN and ORDER BY clauses? The percent signs make sense in the LIKE clause, because in such clauses the percent sign will be used as a wildcard (matching 0 or more instances of any character). In other places, it will be treated as a literal percent sign (and %2010-04-27% isn't a valid date).

      4. Why are you using a string in your ORDER BY clause? Every row will be ordered by a static string... doesn't make much sense. Perhaps you wanted to use a date field in the database instead?

      5. Your BETWEEN syntax is wrong. You're supposed to give MySQL something to compare, e.g.:

        WHERE date_field BETWEEN 'date1' AND 'date2'

        In other words, you're missing an 'AND' (to join both the LIKE and BETWEEN conditions of the WHERE clause) as well as the field name that you're trying to compare in the BETWEEN clause.

      EDIT: 6. What halojoy said above about an ending quote for the whole string. :p

        Thank you guys, you are the best

          Write a Reply...