SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Concatenation

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Concatenation

    How do I properly concatenate a variable in a SQL query? This is what I currently have:

    PHP Code:
    $sql 'SELECT image, name, description FROM phpbb_anime WHERE image = '.$animeimg.''

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,017
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    you need to make sure that you generate single quote delimiters for your image string

    when it reaches mysql, it wants to look like this --
    Code:
    WHERE image = 'foobar'
    of course, that's assuming image is a character column, not a numeric column

    if it's a numeric column, and $animeimg is supposed to be an integer, then you should not generate quotes into the sql

    you can tell if your statement is correct by echoing $sql before you execute it
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do I need I need to put the actual SQL statement in double or single quotations?

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,017
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Hikaru Kagi View Post
    Do I need I need to put the actual SQL statement in double or single quotations?
    can't help you with that, sorry, i don't do php

    what did the echo of $sql produce?
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I can't particularly echo anything because I keep getting a SQL error

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,017
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    if you're getting an sql error, you must've executed the $sql string using mysql_query() or whatever the command in php is called

    could you do the echo before that, please

    also, it might help if i could see the error message
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,904
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sql "SELECT image, name, description FROM phpbb_anime WHERE image = '$animeimg'";
    echo 
    $sql
    What is output by the echo?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I echo that out it just outputs the code.

  9. #9
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,017
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    what happens if you submit the SELECT statement, with an appropriate value substituted for $animeimg, to mysql directly?
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •