Jump to content
Larry Ullman's Book Forums

Variables In A Select Statement


Recommended Posts

Case #1:

 

Variable created from a POST:

$identity = $_POST['soarid'];

 

SELECT statement works just fine:

$result = mysql_query("SELECT * FROM faculty WHERE id = $identity") or die("Faculty ID Not Found");
 

 

Variable created from a db field:

    $fac_school = $row["school"];

Variable content verified ok

 

Attempt to use that variable in a SELECT statement:

$result = mysql_query("SELECT * FROM position WHERE school = $fac_school") or die("Not found");
Does not work!

 

Forcing the variable value thusly:

$fac_school = "SOBM";

$result = mysql_query("SELECT * FROM position WHERE school = $fac_school") or die("Not found");
Still does not work!

 

But this works:

$result = mysql_query("SELECT * FROM position WHERE school = 'SOBM' ") or die("Not found");

 

 

Case #2:

Similar to above but using INSERT INTO.  The insert fails when variable values are used but ok when strings are used for values.

 

All examples int the "PHP and MySQL" book use strings and not variables.  What am I missing?

Link to comment
Share on other sites

 Share

×
×
  • Create New...