PHP mysql在同一页面上选择数据多个表

时间:2012-12-05 06:16:39

标签: php mysql

有两个这样的mysql表:

表1

  

- 编号----- CONFIG_NAME
  --1 ----- OS
   - 2 -----控制面板
  --3 -----带宽

表2

  

id - config_id --- config_Option_name ---价格
  1 -------- 1 ------------- Windows 2008 -------- 20.00
  2 -------- 1 ------------- CentOs 5 ---------------- 0.00
  3 -------- ------------- 2 WHM /的cPanel ----------- 30.00
  4 -------- ------------- 2 -------------------的Plesk 50.00


现在我想像这样展示他们:

  

OS
  Windows 2008 = 20.00
  CentOs 5 = 00.00
  控制面板
  whm / cPanel = 30.00
  Plesk = 50.00

请帮我这样做。

3 个答案:

答案 0 :(得分:0)

试试这个:

Select config_name, config_Option_name, price 

from table1 
inner join table2 on (table1.id=table2.config_id)

答案 1 :(得分:0)

为了你的想法:

public $dbserver = '';
public $dbusername = '';
public $dbpassword = '';
public $dbname = '';

function openDb() {
    try {
        $db = new PDO('mysql:host=' . $this->dbserver . ';dbname=' . $this->dbname . ';charset=utf8', '' . $this->dbusername . '', '' . $this->dbpassword . '');
    } catch (PDOException $e) {
        die("error, please try again");
    }
    return $db;
}

function getRecords() {
    $query = "select * from table1 inner join table2 on table1.id=table2.config_id";
        $stmt = $this->openDb()->prepare($query);
        $stmt->execute();
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $rows;
}

$getAllRows = getRecords();

foreach ($getAllRows as $counter => $row) {
  //this is where you format and output stuff
  echo $row[0] . " and " . $row[1] . " and so on<br>";
 }

答案 2 :(得分:0)

这就是我在寻找的东西

  

$ search =“SELECT a,b FROM DB WHERE x LIKE'$ var'LIMIT 0,30”;

  $ result = mysql_query($ search,$ connection);
  如果($结果){
      while($ row = mysql_fetch_array($ result)){
          echo $ row [“a”]。“”。$ row [“b”]。“”;
          $ search2 =“SELECT a,b FROM DB WHERE x ='”。$ row ['a']。“'LIMIT 0,100”;
          $ result2 = mysql_query($ search2,$ connection);
          如果($结果2){
              while($ row2 = mysql_fetch_array($ result2)){
                  echo $ row2 [“a”]。“”。$ row2 [“b”]。“”;
              }
          其他{
              echo“主题搜索失败。”;
              echo mysql_error();
          }
      }
  其他{
      echo“主题搜索失败。”;
      echo mysql_error();
  }