简体   繁体   English

通过使用php从下拉列表中获取值来显示来自两个Mysql表的数据

[英]display data from two Mysql tables by getting values from drop down list using php

I have two tables countries, location whose fields are 我有两个表格国家/地区,其位置为

_countries
    countryid(Primary Key)
    countryname

_location
    locationid(primarykey)
    locationname
    countryid(Foreign key from countries table)

Every thing is done using JavaScript,AJAX and php that when a user selects a country from drop down list, locations against each country will be displaced but the mysql query is not working I am Using the Below Query 使用JavaScript,AJAX和php可以完成所有操作,当用户从下拉列表中选择一个国家/地区时,针对每个国家/地区的位置将被替换,但mysql查询不起作用。

$sql="SELECT _location.locationname, _countries.countryname FROM _location 
INNER JOIN _countries ON _location.countryid='".$q."'";

//$q is the countryid selected from drop down list i got it through javascript and php

The drop down list is populated from the countries table My Question is when a user selects a country name from the drop down list what will be the mysql query that fetch the location name against each country name and display data like this 下拉列表是从国家表中填充的。我的问题是,当用户从下拉列表中选择一个国家名称时,将是mysql查询,它将针对每个国家名称获取位置名称并显示类似这样的数据

||Location name||Country Name||
  Islamabad       Pakistan
  Karachi         Pakistan
 $sql = "SELECT l.locationname, c.countryname
FROM
_countries c
LEFT JOIN _location l ON c.countryid = l.countryid
WHERE c.countryid = ".(int)$q;

You want to fetch data from 2 tables and when you using join in this case you must use left join to reach your result change your inner join to left join 您想从2个表中获取数据,在这种情况下,当您使用联接时,必须使用左联接才能达到结果,将内部联接更改为左联接

I think it works 我认为它有效

$sql="SELECT _location.locationname, _countries.countryname FROM _location 
LEFT JOIN _countries ON _location.countryid='".$q."'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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