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 9 of 9
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    64
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Is this correct syntax?

    PHP Code:
    $sql "SELECT * FROM 'users' WHERE 'username'='{$username}'"

    Im not sure because im getting errors from this

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by westes View Post
    PHP Code:
    $sql "SELECT * FROM 'users' WHERE 'username'='{$username}'"

    Im not sure because im getting errors from this
    what errors? post the message.

    regards

  • #3
    New Coder
    Join Date
    Aug 2008
    Posts
    64
    Thanks
    11
    Thanked 0 Times in 0 Posts
    The code above

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by westes View Post
    The code above
    the code above is the query not the error message. You said that give you errors. What errors?

    regards

  • #5
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    I should say that I'm in the process of learning mySQL and PHP. Though I might be at mistake, but I believe the syntax is erroneous, and should have been:
    PHP Code:
    $sql "SELECT * FROM 'users' WHERE 'username'='".$username."'"
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by rangana View Post
    I should say that I'm in the process of learning mySQL and PHP. Though I might be at mistake, but I believe the syntax is erroneous, and should have been:
    PHP Code:
    $sql "SELECT * FROM 'users' WHERE 'username'='".$username."'"
    if {} is the problem, php can use curly syntax,

    http://www.php.net/manual/en/languag...arsing.complex

    I guess op must say what is his problem if want help,

    regards

  • Users who have thanked oesxyl for this post:

    rangana (08-14-2008)

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Thats right. {} can be wrapped around variables within strings, thought you'll likely use it more for array indices within double quoted text.
    Are you actually executing this query? This is just a string representation of you're query, you need to run it though a mysql_query or whatever database querying function you are using to create a resultset for it. Once you have a resultset you can retrieve records with mysql_fetch_[row/assoc/array/object] methods. I assumed mysql, but you may be using mysqli or mssql or whatever. They are all pretty similar to use.

    Oh, just as a side note. I always write my queries either like how you have it rangana, or using sprintf or prepared statements. Just a habit I got into I suppose :P
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Try:

    PHP Code:
    $sql "SELECT * FROM `users` WHERE username = '".$username."'"

  • #9
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    If you want to quote fields and table names use the backquote, e.g. `users`. This is typically only necessary if these names are reserved for SQL syntax (for example if you have a field named "order", though some DBs handle even these names just fine).

    Values, however, must be quoted using either single or double quotes, e.g. "my value". It is unnecessary to enclose integers in quotes (but it doesn't hurt either).

    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.


  •  

    Posting Permissions

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