开发者

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 t开发者_运维问答hing 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

$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

||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

I think it works

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜