php - What is the proper syntax for inserting variables into a SELECT statement? -


i believe have simple syntax problem in sql statement. if run code, error in database query.

$user = $_get['linevar']; echo $user;  // testing - url variable echos correctly $sql = "select * `useraccounts` `name` = $user"; $result = mysql_query($sql) or die("error in db query"); 

if replace $user in $sql string 'actualname' or known record in table, code works fine. using $ variable incorrectly in sql string?

you need surround value you're getting $user quotes, since it's not number:

$sql = "select * `useraccounts` `name` = '$user'"; 

just note, should read on sql injection, since code susceptible it. fix pass through mysql_real_escape_string():

$user = mysql_real_escape_string( $_get['linevar']); 

you can replace or die(); logic bit more informative error message when bad happens, like:

or die("error in db query" . mysql_error()); 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -