the code shown below performs an mysql query using implode() to search for multiple variables in one expression, which i like:
<?
include("connect.php"); // file to connect to db
$states = array('CA','CO','TX');
$states_str = implode("','", $states);
$query="SELECT * FROM table1 WHERE state IN ('$states_str')";
$result=mysql_query ($query);
while($row = mysql_fetch_array($result)) {
echo $row['state'];
}
?>
but i have run into a new issue. as you can see from the above 'states' is hard-coded.
i want the user to be able to select the states they want to pull, have that choice saved to a separate table in the DB, and then have that list read into the 'states' array shown above. Something like this:
<?
include("connect.php"); // file to connect to db
$sql_gs="SELECT states_c FROM states WHERE id='1'";
$result_gs=mysql_query($sql_gs);
$row_gs = mysql_fetch_row($result_gs);
$gcs = $row_gs[0];
$states = array($gcs);
$states_str = implode("','", $states);
$query="SELECT * FROM table1 WHERE state IN ('$states_str')";
$result=mysql_query ($query);
while($row = mysql_fetch_array($result)) {
echo $row['state'];
}
?>
but this code throws an error.
what is saved to the states table noted above, and what is contained in the states_c field, a VARCHAR field, is this:
'CA','CO,'TX'
so the '$gcs' variable in the code above ...
echo $gcs; // result 'CA','CO,'TX'
in other words, the text of $gca (for 'get chosen states') is exactly the same as the text in the hard-coded version above that works
but when i try to place the results of the $gca variable, the exact same text, i get an error, no data is pulled, because in this statement
$states = array($gcs); // $gcs reads exactly as 'CA','CO,'TX'
the data is not read, whereas in this one
$states = array('CA','CO,'TX');
the states are recognized and the code fires.
what is the issue with trying to place the same text in this line of code via a named variable as opposed to 'spelling it out' in a hard coded fashion?
TIA
===================
UPDATE
I tried this:
<?
include("connect.php"); // file to connect to db
$sql_gs="SELECT states_c FROM states WHERE id='1'";
$result_gs=mysql_query($sql_gs);
$row_gs = mysql_fetch_row($result_gs);
$gcs = $row_gs[0];
$states = explode("','", $gcs); // replaces $states = array('CA','CO','TX');
$states_str = implode("','",$states);
$query="SELECT * FROM table1 WHERE state IN ('$states_str')";
$result=mysql_query ($query);
while($row = mysql_fetch_array($result)) {
//echo $row['mail_state'];
}
?>
but still no result ...