Forum Moderators: coopster
I have a form sending parameters to a following page in the url. On that page, the array is called $ID, and will have max 4 data points ($ID[0], $ID[1], $ID[2], $ID[3]). The values behind these are product IDs. I want to add these variables in SQL queries. Heres how DW makes a recordset (SQL query) with a server variable of 6 (just as an example):
$colname_rsMod0 = "-1";
if (isset($_SERVER['6'])) {
$colname_rsMod0 = $_SERVER['6'];
}
mysql_select_db($database_BBAddict, $BBAddict);
$query_rsMod0 = sprintf("SELECT * FROM bb_data WHERE id = %s", GetSQLValueString($colname_rsMod0, "int"));
$rsMod0 = mysql_query($query_rsMod0, $BBAddict) or die(mysql_error());
$row_rsMod0 = mysql_fetch_assoc($rsMod0);
$totalRows_rsMod0 = mysql_num_rows($rsMod0);
My question: I wanted to replace the 6 (in this example) with $ID[0] as this is the ID that the user chose on the form, however this does not want to work. Why?
Thanks, Adam
To make sure I am doing this correctly, you're suggesting changing this:
$colname_rsMod0 = "-1";
if (isset($_SERVER[$ID[0]])) {
$colname_rsMod0 = $_SERVER[$ID[0]];
}
mysql_select_db($database_BBAddict, $BBAddict);
$query_rsMod0 = sprintf("SELECT * FROM bb_data WHERE id = %s", GetSQLValueString($colname_rsMod0, "int"));
$rsMod0 = mysql_query($query_rsMod0, $BBAddict) or die(mysql_error());
$row_rsMod0 = mysql_fetch_assoc($rsMod0);
$totalRows_rsMod0 = mysql_num_rows($rsMod0);
to this:
$myid = $ID[0];
if (isset($_SERVER[$myid])) {
$colname_rsMod0 = $_SERVER[$myid];
}
mysql_select_db($database_BBAddict, $BBAddict);
$query_rsMod0 = sprintf("SELECT * FROM bb_data WHERE id = %s", GetSQLValueString($colname_rsMod0, "int"));
$rsMod0 = mysql_query($query_rsMod0, $BBAddict) or die(mysql_error());
$row_rsMod0 = mysql_fetch_assoc($rsMod0);
$totalRows_rsMod0 = mysql_num_rows($rsMod0);
or alternatively:
if (isset($_SERVER[{$ID[0]}])) {
$colname_rsMod0 = $_SERVER[{$ID[0]}];
}
mysql_select_db($database_BBAddict, $BBAddict);
$query_rsMod0 = sprintf("SELECT * FROM bb_data WHERE id = %s", GetSQLValueString($colname_rsMod0, "int"));
$rsMod0 = mysql_query($query_rsMod0, $BBAddict) or die(mysql_error());
$row_rsMod0 = mysql_fetch_assoc($rsMod0);
$totalRows_rsMod0 = mysql_num_rows($rsMod0);
so far so good? The first option doesnt throw any errors, but also no results. the second option throws this:
Parse error: parse error, unexpected '{', expecting ']' in /www/htdocs/w008bdbc/Compare.php on line 51
Line 51 is this line:
$colname_rsMod0 = $_SERVER[{$ID[0]}];
and the brackets dont look out of place to me....
If, generally speaking, array notations cant be included within other arrays, how could I create multiple separate queries, each based on a URL parameter?
Thanks, Adam
mysql_select_db($database_BBAddict, $BBAddict);
$query_rsMod0 = "SELECT * FROM bb_data WHERE id = $ID[0]";
$rsMod0 = mysql_query($query_rsMod0, $BBAddict) or die(mysql_error());
$row_rsMod0 = mysql_fetch_assoc($rsMod0);
$totalRows_rsMod0 = mysql_num_rows($rsMod0);
its actually much simpler than DW wanted to make it :)