I am a novice and using Dreamweaver-composed PHP. I have an issue where I am trying to change a search page (currently using an OR stmt) to accommodate the possibility of multiple search parameters, and if so, to dynamically build an AND statement for the SQL. The code I have pieced together for this "logic" is below. I am not sure if it will provide enough context for the problem or not. I suspect I am getting an "unexpected T_IF..." error because the sprintf doesn't work with what comes after it. Any suggestions would be appreciated.
BTW, the error happens on what is the second line below:
$query_rsAny = sprintf("SELECT * FROM ethno WHERE "
if (!empty($_GET['Department'])) $query_rsAny .= "ethno.Department = %s AND");
if(!empty($_GET['Item'])) $query_rsAny .= "ethno.Item = %s AND ";
if(!empty($_GET['Country'])) $query_rsAny .= "ethno.Country = %s AND ";
if(!empty($_GET['CultureTribe'])) $query_rsAny .= "ethno.CultureTribe = %s AND ";
if(!empty($_GET['MakerArtist'])) $query_rsAny .= "ethno.MakerArtist = %s AND ";
if(!empty($_GET['Age'] )) $query_rsAny .= "ethno.Age = %s AND ";
$query_rsAny = substr($query_rsAny, 0, -4);
GetSQLValueString($colDepartment_rsAny, "text"),GetSQLValueString($colItem_rsAny, "text"),GetSQLValueString($colAccessionCombo_rsAny, "text"),GetSQLValueString($colMakerArtist_rsAny, "text"),GetSQLValueString($colAge_rsAny, "text"),GetSQLValueString($colImagenameNew_rsAny, "text"),GetSQLValueString($colMakerArtistNotes_rsAny, "text"),GetSQLValueString($colDimensions_rsAny, "text"),GetSQLValueString($colCountry_rsAny, "text"),GetSQLValueString($colCultureTribe_rsAny, "text"));
$query_limit_rsAny = sprintf("%s LIMIT %d, %d", $query_rsAny, $startRow_rsAny, $maxRows_rsAny);
$rsAny = mysql_query($query_limit_rsAny, $connEthno) or die(mysql_error());
Is my problem with sprintf or general Dreamweaver PHP?
Moderator: General Moderators
Re: Is my problem with sprintf or general Dreamweaver PHP?
Yeah... That kind of syntax just doesn't work - try something else. The logic for the code doesn't work either, actually: if one part is left out the you lose a %s placeholder and all the arguments get shifted up by one.pfisher wrote:I suspect I am getting an "unexpected T_IF..." error because the sprintf doesn't work with what comes after it.
If you're manually building a query like that I'd just as soon manually build the query. As in without sprintf.