Jamesking56 Posted February 19, 2010 Share Posted February 19, 2010 Hi,I have made a function in PHP to grab settings from a mysql table. But I keep getting errors on it?Can someone tell me whats wrong with it? function setting ( $key ) { $tmp = mysql_query("SELECT value FROM settings WHERE key = '".$key."' LIMIT 1") or die(mysql_error()); $tmp = mysql_fetch_assoc($tmp); return $tmp['value']; } And here is the error I am getting: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key = 'site_name' LIMIT 1' at line 1 Link to comment Share on other sites More sharing options...
boen_robot Posted February 19, 2010 Share Posted February 19, 2010 I think the word "key" may be a keyword (excuse the pun).You should use ` around that word to prevent that, like so: "SELECT value FROM settings WHERE `key` = '". mysql_real_escape_string($key)."' LIMIT 1" Note that I have also included a MySQL escaping function to prevent any possible SQL injections.On a side note, it's a good practice to also keep the link identifier around, and use it as the second argument of every MySQL related function. Link to comment Share on other sites More sharing options...
Jamesking56 Posted February 19, 2010 Author Share Posted February 19, 2010 It works perfectly thank you! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.