Forum Moderators: coopster

Message Too Old, No Replies

Usind $var within DW SQL query

         

oxidetones

1:48 am on Aug 14, 2007 (gmt 0)

10+ Year Member



Hey folks, have another DW/PHP/SQL question...

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

Habtom

5:26 am on Aug 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If this is what you tried:

if (isset($_SERVER['$ID[0]'])) {
$colname_rsMod0 = $_SERVER['$ID[0]'];
}

Try the following:

if (isset($_SERVER[$ID[0]])) {
$colname_rsMod0 = $_SERVER[$ID[0]];
}

oxidetones

1:27 pm on Aug 14, 2007 (gmt 0)

10+ Year Member



thanks, that doesnt seem to work either though. Im not getting any sql error messages, but calling that with <?php echo $row_rsMod0['Type'];?> or anything else doesnt work...

oxidetones

10:30 pm on Aug 16, 2007 (gmt 0)

10+ Year Member



nobody have an idea on this? All Im trying to do is create a new query based on a parameter from a url...

jatar_k

2:28 am on Aug 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$myid = $ID[0];
if (isset($_SERVER[$myid])) {
$colname_rsMod0 = $_SERVER[$myid];
}

you can't use the array notation inside array notation, the above should work and this might work as well, though I am not totally sure

if (isset($_SERVER[{$ID[0]}])) {
$colname_rsMod0 = $_SERVER[{$ID[0]}];
}

oxidetones

5:12 am on Aug 17, 2007 (gmt 0)

10+ Year Member



hey jatar, thanks for jumping in.

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

oxidetones

11:53 pm on Aug 17, 2007 (gmt 0)

10+ Year Member



so, I was playing around with this some more and got it to work, although more by luck than judgement. the following code works:

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 :)

jatar_k

4:46 pm on Aug 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice work oxidetones

sorry I was away for a bit