skip to Main Content

There are two mysql tables like this:

table 1

–id—–config_name
–1—–OS
–2—–Control Panel
–3—–Bandwidth

table 2

id — config_id— config_Option_name — Price
1——–1————-Windows 2008——–20.00
2——–1————-CentOs 5—————-0.00
3——–2————-whm/cPanel———–30.00
4——–2————-Plesk——————-50.00


Now I want to show them like this:

OS
Windows 2008 = 20.00
CentOs 5 = 00.00
Control Panel
whm/cPanel= 30.00
Plesk = 50.00

Please help me to do that.

3

Answers


  1. Try this :

    Select config_name, config_Option_name, price 
    
    from table1 
    inner join table2 on (table1.id=table2.config_id)
    
    Login or Signup to reply.
  2. To get you an idea:

    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>";
     }
    
    Login or Signup to reply.
  3. this is the thing what i was searching for

    $search = “SELECT a,b FROM DB WHERE x LIKE ‘$var’ LIMIT 0 , 30”;
    $result = mysql_query($search, $connection);
    if($result){
    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);
    if($result2){
    while ($row2 = mysql_fetch_array($result2)) {
    echo $row2[“a”].” “.$row2[“b”].” “;
    }
    } else {
    echo “Subject search failed.”;
    echo mysql_error();
    }
    }
    } else {
    echo “Subject search failed. “;
    echo mysql_error();
    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search