gpt4 book ai didi

php - SQL查询取决于同一页面上php中另一个结果

转载 作者:行者123 更新时间:2023-11-30 23:25:58 26 4
gpt4 key购买 nike

我正在尝试编写一个查询,从我的数据库中获取火车列表,用户将输入真实姓名,第一个查询将获取车站代码或“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']);

}
?>

最佳答案

如果您继续阅读 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”

关于php - SQL查询取决于同一页面上php中另一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13336980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com