Jump to content

Mysql: WHERE troubles

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
This query returns

Quote

Unknown column 'bbqroast' in 'where clause'
(if $username is set to bbqroast)
SELECT * FROM users WHERE Username = $username

Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#2
nomaden

nomaden

    Newbie

  • Members
  • PipPip
  • 10 posts
Quotes should be added around $username variable


SELECT *  FROM users WHERE Username = '$username'



#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Yes, you should include quotes over any string value such as a date or username. No quotes are only for tables and numeric values, such as their numeric user ID or a UNIX date.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
Thanks..
I have used this method before with no quotes and that was ok- i avoid quotes cause they have caused me LOTS of trouble before.
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#5
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Don't forget to escape $username with mysql_real_escape_string to prevent SQL injections: PHP: SQL Injection - Manual

If you are using PHP 5, then you can use this:


$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');

$stmt = $mysqli->prepare("SELECT * FROM users WHERE Username = ?")

$stmt->bind_param('s', $username);

$stmt->execute();

or some Database Access Layer that will further simplify your code, for example, with Zend_Db above code will look like:

$db = Zend_Db::factory('Pdo_Mysql', $parameters);

$result = $db->fetchOne('SELECT * FROM users WHERE Username = ?', $username);



#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
with MySQL, you should use aphostrophes, it's needed for strings, but good also for numbers, as it helps a little against SQL injection as well.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall