SQL查询依赖于同一页面上php中另一个查询的结果
我试图编写一个查询,从我的数据库中获取列车列表,用户将输入真实姓名,第一个查询将获取车站代码或“tiploc”,然后在第二个查询中使用它。出于某种原因,我没有得到任何回报,我确信这与从fetch获取的数据有关,因为如果我硬编码tiploc,它工作得很好。我在php方面相当弱,所以任何帮助都将是非常好的!谢谢SQL查询依赖于同一页面上php中另一个查询的结果,php,mysql,Php,Mysql,我试图编写一个查询,从我的数据库中获取列车列表,用户将输入真实姓名,第一个查询将获取车站代码或“tiploc”,然后在第二个查询中使用它。出于某种原因,我没有得到任何回报,我确信这与从fetch获取的数据有关,因为如果我硬编码tiploc,它工作得很好。我在php方面相当弱,所以任何帮助都将是非常好的!谢谢 <? mysql_connect("localhost","root","XXXXXX") or die ("No connection could be made to the O
<?
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\u fetch\u assoc。我假设tiploc_代码是数据库中的一个字段。因此,当您查询数据库时,它将返回一个对象。你可以用
$tipoc = $reuslt1[0]->tiploc_code
如果只有一排。如果有多行,请使用。
$count=count($result1)
($i=0;$itiploc\U代码)的
$query2=“选择allstations.C,locations.public\u出发
从'locations',allstations,schedules\u cache,schedules
其中locations.id=schedules\u cache.id
和schedules_cache.id=schedules.id
和“2012-11-11”之间。日期从和日期到
和locations.tiploc\u代码=\“$tipoc\”
和位置。公共_出发>=1600
和地点。公共_出发两件事:
首先,您的代码中有一个输入错误。在第一个查询的第一个while循环中,您有一个名为$tipoc的变量,而不是$tiploc。因此,在第二个查询中不会有结果
其次,您应该将第二个查询的逻辑放入第一个查询的循环中。这样,只有在第一个查询返回结果时才执行第二个查询。如果您只希望第一个记录中有一条记录,则可以将逻辑从while循环更改为if语句,如下所示:
if($row = mysql_fetch_assoc($result1)){
$tiploc=$row['tiploc_code'];
...
include your second query here inside the if statement.
您正在覆盖$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']);
}
?>
我真的希望这不是你真正的数据库密码=o\ahhh,谢谢,尽管这个特定的示例仅在一台机器上本地,无法在外部访问。谢谢!很有魅力,是的,它只有一行。
<?
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']);
}
?>