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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2014
    Posts
    129
    Thanks
    39
    Thanked 1 Time in 1 Post

    Problem with query and a string

    PHP Code:
    $query"SELECT * from db WHERE ".'`[id]`'." = 'EXPORT-325' "
    This one works fine, the data are found.

    PHP Code:
    $id 'EXPORT-325'
    echo 
    $id.'<br>';

    $query"SELECT * from db WHERE ".'`[id]`'." = " $id 
    In this way there is an error:
    EXPORT-325
    Unknown column 'EXPORT' in 'where clause'

    Does somebody see the problem? I tried it with escaping .`$id` but nothing helps.
    Thanks for your help!
    Last edited by asterix22; Feb 9th, 2019 at 04:27 PM.

  2. #2
    Regular Coder
    Join Date
    Oct 2015
    Posts
    427
    Thanks
    1
    Thanked 54 Times in 51 Posts
    You've got a mismatch in the quotes, there is a missing double quote at the end of the line.

    You don't need to change from double to single quotes, just create the string using single quotes.

  3. #3
    Regular Coder
    Join Date
    Dec 2014
    Posts
    129
    Thanks
    39
    Thanked 1 Time in 1 Post
    PHP Code:
    $query'SELECT * from db WHERE '.'`[id]`'.' = ' $id 
    PHP Code:
    $query'SELECT * from db WHERE '.'`[id]`'.' = ' $id.' ' 
    it doesn't help... how do you mean?

    In the first one I corrected it now. There was a missing quote.
    But the others are the problem - what's wrong with them?

  4. #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,255
    Thanks
    3
    Thanked 553 Times in 538 Posts
    Where is the $id value coming from? If it's coming from an external/unknown source, you shouldn't be putting it directly into the sql query statement. You should be using a prepared query instead, with a place-holder in the sql query for the value, then supply the value when the query gets executed. This will solve one of your quoting problems because with a prepared query, there's no variable, no single-quotes around the value, and no concatenation dots in the sql query statement.

    Next, why are you concatenating the id column into the sql query statement? That's unnecessary clutter.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  5. #5
    Regular Coder
    Join Date
    Dec 2014
    Posts
    129
    Thanks
    39
    Thanked 1 Time in 1 Post
    The $id value is coming from an other sql-query. I'm sure that's something like EXPORT-123
    I have to look for the datas which are in the column [id] with the ID $id. That's why I have to do it like this.

    Why the query stops in the moment of the '-' - but only if the variable is there - not if I have written the same thing?

  6. #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,255
    Thanks
    3
    Thanked 553 Times in 538 Posts
    You would use a single JOIN query to get related data.

    As to the problem you are having with the quotes. A String value must be surrounded by single-quotes, otherwise it is treated as an identifier, i.e. a column name. Regardless of the string being typed in the sql query statement or being supplied via a php variable, the value must be surrounded by single-quotes. Don't use concatenation at all, you are mixing too much different php/sql syntax that's just creating a mess.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  7. Users who have thanked CFMaBiSmAd for this post:

    asterix22 (Feb 9th, 2019)

  8. #7
    Regular Coder
    Join Date
    Dec 2014
    Posts
    129
    Thanks
    39
    Thanked 1 Time in 1 Post
    Thanks! I'll try with JOIN...
    I do not understand yet why it works directly, but not with the variable. It's because of the single-quotes and the complicated column name?


 

Tags for this Thread

Posting Permissions

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