cant pass a variable to a mysql_query
Posted: Sat Jun 07, 2008 11:05 am
Help! Im just learning PHP. I'm trying to write a script that receives a variable "NAME" from a html form,
which is a field in a mysql database.
my script receives 'NAME' as indicated by printing it out.(echo $name) BUT,
all I get from the mysql_query are warnings that the arguments supplied are not valid. When I use a real field name for the
query such as ".....WHERE 'Holly'..." the script runs fine and returns all data sucessfully
Why cant I pass a $name to the mysql_query ???
Thanks in advance,
Bob in San Diego
script:
<?php
mysql_connect("localhost", "username", "pw") or
die("Could not connect: " . mysql_error());
$name = strip_tags(trim($_POST['NAME']));
echo $name;
mysql_select_db("test");
$result = mysql_query("SELECT NAME, State, COLOR FROM testa WHERE NAME = $name");
mysql_real_escape_string($name);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
printf("ID: %s Name: %s COLOR: %s", $row["NAME"], $row["State"], $row[COLOR]);
mysql_free_result($result);
?>
which is a field in a mysql database.
my script receives 'NAME' as indicated by printing it out.(echo $name) BUT,
all I get from the mysql_query are warnings that the arguments supplied are not valid. When I use a real field name for the
query such as ".....WHERE 'Holly'..." the script runs fine and returns all data sucessfully
Why cant I pass a $name to the mysql_query ???
Thanks in advance,
Bob in San Diego
script:
<?php
mysql_connect("localhost", "username", "pw") or
die("Could not connect: " . mysql_error());
$name = strip_tags(trim($_POST['NAME']));
echo $name;
mysql_select_db("test");
$result = mysql_query("SELECT NAME, State, COLOR FROM testa WHERE NAME = $name");
mysql_real_escape_string($name);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
printf("ID: %s Name: %s COLOR: %s", $row["NAME"], $row["State"], $row[COLOR]);
mysql_free_result($result);
?>