我试图从函数中调用 $row (数据库数组),但基于变量调用列名。
这是完整的代码...您可以在以下位置查看结果
http://tlcs.stuart-pinfold.co.uk/test.php?id=20
和
http://tlcs.stuart-pinfold.co.uk/test.php?id=21
<?php
include("includes/db.php"); // includes all the db connections
$id = $_GET['id'];
$sql = "SELECT * FROM users WHERE UserID='".$id."'";
$set = mysql_query($sql);
$row = mysql_fetch_array($set);
function checkAvailability($day)
{
$check = "UserCommute".$day;
if($row[$check]=="1")
{
echo '<img src="/tick.jpg" alt="Available on this day" />';
}
else
{
echo '<img src="/cross.jpg" alt="Not available on this day" />';
}
echo " = ".$check."<br/>";
}
echo "Full Name:<br/>".$row['UserFullName']; // this works perfectly
echo "<br/><br/>Using the function...<br/>";
echo checkAvailability('Mon');
echo checkAvailability('Tue');
echo checkAvailability('Wed');
echo checkAvailability('Thu');
echo checkAvailability('Fri');
echo checkAvailability('Sat');
echo checkAvailability('Sun'); // these always return a cross even when the database entry is 1
echo "<br/>Using hard-coded row values...<br/>";
echo $row['UserCommuteMon']." = UserCommuteMon<br/>";
echo $row['UserCommuteTue']." = UserCommuteTue<br/>";
echo $row['UserCommuteWed']." = UserCommuteWed<br/>";
echo $row['UserCommuteThu']." = UserCommuteThu<br/>";
echo $row['UserCommuteFri']." = UserCommuteFri<br/>";
echo $row['UserCommuteSat']." = UserCommuteSat<br/>";
echo $row['UserCommuteSun']." = UserCommuteSun<br/>"; // these work perfectly, returning 0s or 1s, matching the database
?>
它只返回十字架,从不蜱。
我哪里错了?