Can you use variables in mysql_fetch_array output? (PHP)?

  • Thread starter Thread starter Rich21
  • Start date Start date
R

Rich21

Guest
I'm trying to make a depth chart for a sports game I'm trying to make. I don't have it in front of me but the code looks something like this:

$query = "SELECT Team, $pos1, $pos2, $pos3, $pos4 FROM depth WHERE Team = $teamID";
$result = mysql_query($query);
$record = mysql_fetch_array($result);
// All the above works fine
$output1 = $record[$pos1];
$output2 = $record[$pos2];
$output3 = $record[$pos3];
$output4 = $record[$pos4];

$output comes out null whether or not the field is. If I try $output = $record['$pos1'];(with apostrophes) it will just echo what $pos1 is(ie if $pos1 = C, then it will just echo "C") not what is in the field itself. Is it possible to do what I'm trying? Is there a more efficient way to do it?

Any help is appreciated. Thank you.
 
Your notation is wrong.

Assuming the table named depth has columns pos1, pos2, pos3 and pos4:

$rs = mysql_query( "SELECT pos1, pos2, pos3, pos4 FROM depth WHERE Team = $teamID" ) or die(mysql_error());
if(mysql_num_rows($rs) == 0) {
echo "No match found"
}
else {
$row = mysql_fetch_array($rs);
echo $rs['pos1'];
echo $rs['pos2'];
echo $rs['pos3'];
echo $rs['pos4'];
}
 
Back
Top