Jump to content

Mysql Function Erroring?


Jamesking56
 Share

Recommended Posts

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...