从具有代码值的数据库中获取结果

时间:2017-08-25 10:21:29

标签: php mysql database

我想从数据库表中检索值并在php文件中显示它们。 数据库表名是profiledetails,它现在有6个字段(id,Name,dob,gender,district和caste)但是性别的值为男性为1,女性为2,变性为3,同样地区值也是以数字格式给出。还有另一个用于存储值的区域表。同样地,种姓价值存储为1表示印度语,2表示穆斯林,3表示穆斯林,并且将有一个单独的表用于存储在数据库中命名为castetable的种姓值。如何让它显示数据库中的所有值像这样的PHP:

$sql="SELECT a.id,a.name,a.dateofbirth,a.gender,ca.religion As Category,s.district FROM profiledetails a Left join castetable ca ON ca.id=a.religion Left join districttable s ON s.district=a.district  Where a.id='$is';";

$是值具有特定学生的ID。

<table border="1">
  <tr>
    <th> ID</th>
<th>Name</th>
    <th>DOB</th>
    <th>Gender</th>
    <th>District</th>
    <th>Caste</th>

如果性别值为1,则应在php页面中将其打印为男性 注意:没有用于存储性别值的单独表格。它表示男性为1,女性为2,但不是在任何地方写入或存储。

<tr>
    <td>123456789</td>
    <td>XYZ</td>
    <td>12/02/1999</td>
    <td>Female</td>
    <td>New York </td>
    <td>Muslim</td>
  </tr>
</table>

我已经编写了所有数据库连接代码和查询以获取结果,但我很困惑如何比较这些值(性别,区域和宗教)并打印正确的值。 请帮助我,并提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

尝试在查询下方运行:

$sql="SELECT a.id,a.name,a.dateofbirth,(case g.gender when 1 then 'Male' when 2 then 'female' when 3 then 'transgender' else null end) as gender ,ca.religion As Category,s.district FROM profiledetails a, castetable ca, districttable s where a.religion=ca.id AND a.district=s.district  and a.id='$is';";

我想这会奏效。

答案 1 :(得分:0)

$ sql =“SELECT a.id,a.name,a.dateofbirth, 例如a.gender = 1然后'男'当性别= 2然后'女'当性别= 3然后'变性'其他无效END作为性别 ca.religion作为类别,s.district  来自profiledetails a,castetable ca,districttable s 其中a.religion = ca.id AND a.district = s.district和a.id ='$ is'“;