Jump to content

Pass value from database field to PHP parameter


lucerias

Recommended Posts

[code]
$someweight = 10;
$sql = "select * from table where weight < $someweight";
$result = mysql_query($sql) or die(mysql_error());
while ($list = mysql_fetch_array($result)) {
  $rate = $list['rate'];
  $misc = $list['misc'];
  $other = $list['other'];
}
[/code]

please note that if the query returns more than one result, the variables will be overwritten each time until the last pass of the while loop, resulting in only the last row being grabbed. If you are expecting only 1 row returned, then that's fine.  If you are expecting more than one row to be returned, you should meke $rate, $misc, $other arrays like so:

[code]
$someweight = 10;
$sql = "select * from table where weight < $someweight";
$result = mysql_query($sql) or die(mysql_error());
while ($list = mysql_fetch_array($result)) {
  $rate[] = $list['rate'];
  $misc[] = $list['misc'];
  $other[] = $list['other'];
}
[/code]

or better yet just make one multi-dim array like so:

[code]
$someweight = 10;
$sql = "select * from table where weight < $someweight";
$result = mysql_query($sql) or die(mysql_error());
while ($list = mysql_fetch_array($result)) {
  $info[] = $list;
}
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.