How to get values ​​from multiple queries in a single while loop?

In this code, I need to print the name of the train obtained from the request $q1, but in the echo expression I indicated that this does not work. How to print trainname in this code?

$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>";

}
+4
source share
3 answers

The result string is assigned to a variable $row, not a variable $r1(which is the request resource):

while($row = mysql_fetch_array($r1)) 
{  
  $trainname=$row['st_name'];   
} 
+2
source

You are extracting data from a result object, instead you should extract data from a string of variables

while($row = mysql_fetch_array($r1))

    {

    $trainname=$row['st_name'];

    }
+1
source

:

  • mysql_fetch_array , ( )
    • $row = mysql_fetch_array($r1, MYSQL_ASSOC), $row = mysql_fetch_assoc($r1)
  • . ( )
    • the $trainname=$row['st_name'];

, mysqli pdo

0

All Articles