instead of searching a mysql DB for one variable at a time, i want to search for a number of variables, and fit it into the initial query string
so instead of
$query="SELECT * FROM table1 WHERE state = 'CA' OR state = 'CO' OR state = 'TX'";
i want
$states = ("CA,CO,TX");
$query="SELECT * FROM table1 WHERE state = $states";
I have tried
<?
include("connect.php"); // file to connect to db
$states = array(CA,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 get this error message
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in ... on line 11
this is line 11
while($row = mysql_fetch_array($result)) {
however, if i use this code, i get a result, only to show that when i switch back to the old conventional query it works
<?
include("connect.php"); // file to connect to db
$states = array(CA,TX);
$states_str = implode(",", $states);
$query="SELECT * FROM table1 WHERE state = 'CA'";
$result=mysql_query ($query);
while($row = mysql_fetch_array($result)) {
echo $row['state'];
}
?>
but then i am bypassing the implode() thing, which i found somewhere else but it is not working for me
mysql_queryin new applications. It's deprecated, dangerous if used incorrectly, and is being removed from future versions of PHP. A modern replacement like PDO is not hard to learn. A guide like PHP The Right Way will help you avoid making mistakes like this.mysql_queryhas been deprecated for a while now, and widely regarded as obsolete for at least five years. Remember "working fine" is a subjective thing. If your code is riddled with SQL injection bugs, it is really not fine.