简体   繁体   中英

SQL query dependant on result of another in php on the same page

I am trying to write a query that gets a list of trains from my database, the user will enter the real name and the first query will get the code for the station or 'tiploc' and then use it in the second query. For some reason i am not getting anything back, I am certain that it is to do with data the is gotten from the fetch as it works fine if I hardcode the tiploc. I am fairly weak at php so any help would be great! Thanks

<?
 mysql_connect("localhost","root","XXXXXX")
or die ("No connection could be made to the OpenRail 
Database");mysql_select_db("autotrain");

$query1 = "SELECT tiploc_code FROM allstations WHERE c LIKE 'Cradley Heath';";

$result1 =mysql_query($query1);


$tiploc=null;
while($row = mysql_fetch_assoc($result1)){
$tipoc=$row['tiploc_code'];

}
$query2 = "SELECT allstations.C, locations.public_departure
FROM `locations` , allstations, schedules_cache,schedules
WHERE locations.id = schedules_cache.id
AND schedules_cache.id = schedules.id
AND '2012-11-11' BETWEEN schedules.date_from AND schedules.date_to
AND locations.tiploc_code = '$tiploc'
AND locations.public_departure >=1600
AND locations.public_departure <=1700
AND schedules.runs_su LIKE '1'
AND schedules_cache.destination = allstations.tiploc_code
ORDER BY locations.public_departure ASC;";

$result2=mysql_query($query2);

while($row = mysql_fetch_assoc($result2)){
echo($row['C']);

}
?>

If you go on the PHP manual page the use of mysql_fetch_assoc is discouraged. I am assuming that tiploc_code is a field in your DB. So when you query the DB it will return an object. You could use

$tipoc = $reuslt1[0]->tiploc_code

if its only 1 row. If there are multiple rows use. $count = count($result1);

for($i = 0;$i<count;$i++){
$tipoc = $reuslt1[$]->tiploc_code
$query2 = "SELECT allstations.C, locations.public_departure
FROM `locations` , allstations, schedules_cache,schedules
WHERE locations.id = schedules_cache.id
AND schedules_cache.id = schedules.id
AND '2012-11-11' BETWEEN schedules.date_from AND schedules.date_to
AND locations.tiploc_code = \"$tipoc \"
AND locations.public_departure >=1600
AND locations.public_departure <=1700
AND schedules.runs_su LIKE '1'
AND schedules_cache.destination = allstations.tiploc_code
ORDER BY locations.public_departure ASC;";

$result2=mysql_query($query2);

$c = $result2[0]->C;
echo $c;
}

Again i am assuming that there will be only one 'c' for every tiploc

Two things:

First, you have a typo in your code. In your first while loop for the first query you have the variable called $tipoc instead of $tiploc. because of this you'll have no results in you second query.

Second, you should put the logic for your second query inside the loop for the first query. That way you only execute the second query if the first one returns results. If you only expect a single record from the first record you can change the logic from a while loop to an if statement like this:

if($row = mysql_fetch_assoc($result1)){
    $tiploc=$row['tiploc_code'];
    ...
    include your second query here inside the if statement.

You are overwritting $tipoc=$row['tiploc_code'];

Try the following code.

<?
 mysql_connect("localhost","root","Boeing1992")
or die ("No connection could be made to the OpenRail 
Database");mysql_select_db("autotrain");

$query1 = "SELECT tiploc_code FROM allstations WHERE c LIKE 'Cradley Heath';";

$result1 =mysql_query($query1);


$tiploc=null;
$tipocArr = array();
while($row = mysql_fetch_assoc($result1)){
$tipocArr[] =$row['tiploc_code'];

}
$tipoc = implode(",",$tipocArr);

$query2 = "SELECT allstations.C, locations.public_departure
FROM `locations` , allstations, schedules_cache,schedules
WHERE locations.id = schedules_cache.id
AND schedules_cache.id = schedules.id
AND '2012-11-11' BETWEEN schedules.date_from AND schedules.date_to
AND locations.tiploc_code IN '$tiploc'
AND locations.public_departure >=1600
AND locations.public_departure <=1700
AND schedules.runs_su LIKE '1'
AND schedules_cache.destination = allstations.tiploc_code
ORDER BY locations.public_departure ASC;";

$result2=mysql_query($query2);

while($row = mysql_fetch_assoc($result2)){
echo($row['C']);

}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM