在1个数据库中比较2个表sql的echo结果

时间:2015-10-29 03:48:41

标签: php sql arrays

数据库名称:test
表名:员工
structur

+-----+------------+----------+--------+
| id  | name       | Password |  code  |
+-----+------------+----------+--------+
| 11  | John       | test122  |   A1   |
| 12  | Johana     | test124  |   A2   |
| 13  | David      | test125  |   A3   |
| 14  | Anna       | test126  |   A1   |
| 15  | Mike       | test127  |   A5   |
+-----+------------+----------+--------+

表名:code_id
structur

+-------+------------+
| code  | codename   |
+-------+------------+
| A1    | Security   |
| A2    | Manager    |
| A3    | Admin      |
| A4    | guest      |
| A5    | CEO        |
+-------+------------+

我想说欢迎和成功

$strSQL = "SELECT * FROM employee";
    $rs = mysql_query($strSQL);
    while($row = mysql_fetch_array($rs)) {
    $idcode = $row['code'];
      }
    mysql_close();

echo $ idcode
结果:
欢迎,A1

<小时/> 现在我想要 回声 欢迎,经理 要么 欢迎CEO

如何在php上查询?

2 个答案:

答案 0 :(得分:1)

尝试此查询

$strSQL = "select a.*,b.codename from employee as a join code_id as b on a.code=b.code";
    $rs = mysql_query($strSQL);
    while($row = mysql_fetch_array($rs)) {
    $idcode = $row['codename'];
      }
    mysql_close();

答案 1 :(得分:0)

没有查询的更简单方法就是这样,

<?php
function convertCode($code) {
    $codes = [
        "A1" => "Security",
        "A2" => "Manager",
        "A3" => "Admin",
        "A4" => "guest",
        "A5" => "CEO"
    ];
    return $codes[$code];
}
?>

然后在你的循环中,

echo "Welcome " . convertCode($row['code']) . ".";

修改1

您不应该再使用mysql,因为它已被正式弃用。您应该更改为mysqliPDO