Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql error 1

Status
Not open for further replies.

ascikey

Programmer
Feb 18, 2004
127
GB
when i issue the following query i get "Lookup failed: Unknown column 'B753' in 'where clause'" but B753 is a value in the $value var.
does anybody know why this is?
Code:
foreach ($_POST['dvd'] as $value)
{
  # make a SQL query specific to the dvd
 $query = "SELECT * FROM video WHERE video.Code= ". $value;
 $result = mysql_query($query) OR die("Lookup failed: ".mysql_error());
 $row = mysql_fetch_assoc($result);
 # process the record now
 foreach($row as $key=>$val)
 {
    echo 'key is '. $key .' and val is '. $val;
 }
}
thx
 
Is it the space between the period and the "$value"?
 
thx Dweezel but didnt seem to make much difference
Code:
foreach ($_POST['dvd'] as $value)
{
  # make a SQL query specific to the dvd
  $query = "SELECT * FROM video WHERE video.Code= ".$value;
  $result = mysql_query($query) OR die("Lookup failed: ".mysql_error());
  $row = mysql_fetch_assoc($result);
  # process the record now
  foreach($row as $key=>$val)
  {
    echo 'key is '. $key .' and val is '. $val;
  }
}
:(
 
Try writing the =".$value bit like this:
=$value";

As it's in double quotes it should still evaluate your variable properly.

I'm clutching at straws though m8. I'm too tired to be of any use I think [sleeping2]
 
Fortunately this is an easy answer - and basic:

SQL statements require strings to be enclosed in quotes. Otherwise the literal is interpreted as a SQL meta idenfifier, such as a column name.
Lets say the value is B753
Code:
$query = "SELECT * FROM video WHERE video.Code= ". $value;
# evaluates as:
SELECT * FROM video WHERE video.Code = B753
# whereas it should be
$query = "SELECT * FROM video WHERE video.Code= [COLOR=red]'[/color]". $value .[COLOR=red]"'"[/color];
# evaluates as
SELECT * FROM video WHERE video.Code = 'B753'
That's all. If you look at the wrong statement you can see that the SQL interprets B753 as a column name in the specific spot it is in the statement. And, as there is no such column, it fails.
 
i see, again u solve my probem thx very much
 
I had a few problems with that but here is what i ended up with
Code:
$mark =  "'";
$temp =$mark . $value . $mark;
$query = "SELECT * FROM video WHERE video.Code=$temp";
thx all
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top