...

View Full Version : select ids



Radeom
07-15-2012, 02:53 PM
$query = mysql_query("Select name From names Where id = '".$ids."' ") or die(mysql_error());

now my $ids can be:
1
2
ALL

If $ids = ALL , then how it takes all of id-s which is 1 or 2?

gvre
07-15-2012, 03:20 PM
$sql = "SELECT name FROM names";
if ($ids != "ALL")
$sql .= " WHERE id=" . (int)$ids;
$query = mysql_query($sql) or die(mysql_error());

Radeom
07-15-2012, 04:12 PM
it doesn't work :(

Fou-Lu
07-15-2012, 05:19 PM
You'll have to remove the datatype cast to an integer and treat it as a string. We know this since you are containing both strings and numbers in a field (which is probably not a great idea to start with), so it therefore must be a string type. MySQL can be datatype strict if its set that way.


$sql = "SELECT name FROM names";
if ($ids != "ALL")
{
$sql .= " WHERE id='" . mysql_real_escape_string($ids) . "'";
}
$query = mysql_query($sql) or die(mysql_error());



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum