在给定的代码中,我需要打印从查询$q1
获得的train-name,但是在我提供的echo语句中,这不起作用。如何在给定代码中打印 trainname ?
$q1="SELECT st_name FROM tbl_station where st_code='$f'";
$r1=mysql_query($q1);
while($row = mysql_fetch_array($r1))
{
$trainname=$r1['st_name'];
}
$query="SELECT A.train_no AS AA, A.station_id AS AB, A.arrival AS AC, A.dept AS AD,
B.station_id AS AE, B.arrival AS AF, B.dept AS AG FROM TIME AS A,TIME AS B WHERE A.train_no
= B.train_no AND A.station_id ='$f' AND B.station_id ='$t'";
$rs=mysql_query($query);
while($row = mysql_fetch_array($rs))
{
echo "<tr><td>".$row['AA']."</td> <td>".$trainname."</td> <td>" .$row['AC'] ."</td>
<td>".$row['AD'] . "</td><td>".$row['AE'] . "</td><td>".$row['AF'] . "</td><td>"
.$row['AG']. "</td><td>"."<a href='Reservation.php'>Click Me</a><tr><td>";
}
答案 0 :(得分:2)
结果行分配给$row
变量,而不是$r1
变量(查询资源):
while($row = mysql_fetch_array($r1))
{
$trainname=$row['st_name'];
}
答案 1 :(得分:1)
您正在从结果对象中检索数据,而应从变量行
中获取数据while($row = mysql_fetch_array($r1))
{
$trainname=$row['st_name'];
}
答案 2 :(得分:0)
有两个问题:
$row = mysql_fetch_array($r1, MYSQL_ASSOC)
或$row = mysql_fetch_assoc($r1)
$trainname=$row['st_name'];
但是你有明显的安全漏洞,所以你应该研究mysqli或pdo