SQL查询依赖于同一页面上的另一个PHP的结果

时间:2012-11-12 00:29:12

标签: php mysql

我正在尝试编写一个从我的数据库中获取列车列表的查询,用户将输入真实姓名,第一个查询将获取该站的代码或'tiploc',然后在第二个查询中使用它。出于某种原因,我没有得到任何回报,我确信这是与从fetch获得的数据有关,因为如果我硬编码tiploc它工作正常。我在php上相当弱,所以任何帮助都会很棒!感谢

<?
 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']);

}
?>

3 个答案:

答案 0 :(得分:0)

如果您继续使用PHP手册页,则不建议使用mysql_fetch_assoc。我假设tiploc_code是数据库中的一个字段。因此,当您查询数据库时,它将返回一个对象。你可以用

$tipoc = $reuslt1[0]->tiploc_code

如果它只有1排。如果有多行使用。     $ 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;
}

我再次假设每个tiploc只有一个'c'

答案 1 :(得分:0)

两件事:

首先,您的代码中有拼写错误。在第一个查询的第一个while循环中,您有一个名为$ tipoc而不是$ tiploc的变量。因此,您在第二次查询时没有结果。

其次,您应该将第二个查询的逻辑放在第一个查询的循环中。这样,如果第一个查询返回结果,则只执行第二个查询。如果您只期望第一条记录中的单条记录,则可以将逻辑从while循环更改为if语句,如下所示:

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

答案 2 :(得分:0)

你正在覆盖$ topic = $ row ['tiploc 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']);

}
?>