1

我正在尝试编写一个查询,从我的数据库中获取火车列表,用户将输入真实姓名,第一个查询将获取车站或“tiploc”的代码,然后在第二个查询中使用它。出于某种原因,我没有得到任何回报,我确信这与从获取中获取的数据有关,因为如果我对 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']);

}
?>
4

3 回答 3

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'

于 2012-11-12T00:40:31.723 回答
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.
于 2012-11-12T00:40:57.453 回答
0

您正在覆盖 $tipoc=$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']);

}
?>
于 2012-11-12T00:42:09.517 回答