1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: MySQL Help

Discussion in 'Web Design & Development' started by lumentec, Sep 27, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. lumentec

    lumentec Thread Starter

    Joined:
    Mar 23, 2008
    Messages:
    103
    In PHP, should I use
    Code:
    mysql_query("SELECT something FROM table WHERE something=$something")
    or should I add a ' before and after the variable I'm comparing like this:
    Code:
    mysql_query("SELECT something FROM table WHERE something='$something'")
    ?
     
  2. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    If it is a string use the latter, otherwise use the former.

    Also, unless you know what $something is you should escape it.
     
  3. lumentec

    lumentec Thread Starter

    Joined:
    Mar 23, 2008
    Messages:
    103
    As always, thanks for your help. :p
     
  4. lumentec

    lumentec Thread Starter

    Joined:
    Mar 23, 2008
    Messages:
    103
    Actually... you were wrong. Completely. And it took me about 2 hours to figure out how to fix it. If you're going to use a variable inside a MySQL command, it has to be surrounded in ' ' or it won't work properly.
     
  5. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    Can you show me an example of what you mean?
     
  6. lumentec

    lumentec Thread Starter

    Joined:
    Mar 23, 2008
    Messages:
    103
    Code:
    $result = mysql_query("SELECT * FROM user WHERE username='$username'");
    as opposed to:

    Code:
    $result = mysql_query("SELECT * FROM user WHERE username=$username");
     
  7. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    Code:
    mysql_query("SELECT something FROM table WHERE something='$something'")
    Code:
    mysql_query("SELECT something FROM table WHERE something=$something")

    Umm... how did you misunderstand my post?
     
  8. lumentec

    lumentec Thread Starter

    Joined:
    Mar 23, 2008
    Messages:
    103
    It's not a string.
     
  9. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    Then using the former should work fine.

    mysql_query("SELECT something FROM table WHERE something=1")

    is a valid query.
     
  10. lumentec

    lumentec Thread Starter

    Joined:
    Mar 23, 2008
    Messages:
    103
    I was comparing it to a variable.
    You said to use no ' 's if it wasn't a string. A variable is not a string so I didn't use ' 's. That made sense to me too because Javascript and PHP interperate anything inside " " or ' ' to be a string. But, that's not the case.

    No matter if it's a variable or not, it still needs ' 's.
     
  11. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    I'm sorry but that is wrong.

    Only strings need to be quoted.
     
  12. lumentec

    lumentec Thread Starter

    Joined:
    Mar 23, 2008
    Messages:
    103
    No. You're wrong. In a mysql_query() you have to surround variables with ' 's or you'll get an error.
     
  13. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    Wherever you are getting that from, its incorrect.

    It works fine for me.
     
  14. lumentec

    lumentec Thread Starter

    Joined:
    Mar 23, 2008
    Messages:
    103
    I got it from my page which works on that principal. I guess it's possible we're using different versions of PHP... but for me, the only way it will work is if I use ' 's.
     
  15. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/753925

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice