2017-01-13 36 views
1

我是PHP和SQL的新手。我還在學習。 所以,我在我的DB 3個表叫ps_plrps_plr_ids_nameps_plr_victms如何從兩個表中獲取sql數據並打印到一個?

  • ps_plr,我需要列:排名
  • ps_plr_ids_name,我需要列:
  • ps_plr_victms,我需要兩列:殺死 a第二死亡
在所有這些表的

,我有plrid列,將作爲一個基礎。

我需要輸出一個html表格: 排名,名稱,殺死和死亡

我嘗試下面的代碼,可以打印級別和plrid,但我並不真的需要plrid在桌子上,正如我所說,只是作爲一個基地,以找到我所需要的其他表格。

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = "********"; 
$dbname = "psychostats3_1"; 

$mysqli = new mysqli($servername,$username,$password,$dbname); 

/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

$query = "SELECT ps_plr.rank, ps_plr.plrid FROM ps_plr;"; 
$query .= "SELECT ps_plr_ids_name.name FROM ps_plr_ids_name plrnk, ps_plr plrst;"; 
$query .= "WHERE plrid=name"; 


//select task.id, task.name, proj.id, proj.name 
//from tasks task, projects proj 
//where proj.id=task.project_id; 

/* execute multi query */ 
if ($mysqli->multi_query($query)) { 
    do { 
     /* store first result set */ 
     if ($result = $mysqli->store_result()) { 
      while ($row = $result->fetch_row()) { 
       printf("<tr><td>" . $row[0] . "</td>"); 
       printf("<td>" . $row['2'] . "</td></tr>"); 
      } 
      $result->free(); 
     } 
     /* print divider */ 
    } while ($mysqli->next_result()); 
} 

/* close connection */ 
$mysqli->close(); 
?> 
+0

你能PLS顯示兩個表的表晶格結構? –

+1

在'pri_id'上加入三個表,並使用where子句作爲'plrid ='name''id name是字符串。 –

回答

0
+-----------------+----------------+ 
| tutorial_author | tutorial_count | 
+-----------------+----------------+ 
| mahran   |    20 | 
| mahnaz   |   NULL | 
| Jen    |   NULL | 
| Gill   |    20 | 
| John Poul  |    1 | 
| Sanjay   |    1 | 
+-----------------+----------------+ 

讓我們說這是現在執行查詢的表tcount_tbl 和我的另一臺tutorials_tbl

+-------------+----------------+-----------------+-----------------+ 
| tutorial_id | tutorial_title | tutorial_author | submission_date | 
+-------------+----------------+-----------------+-----------------+ 
|   1 | Learn PHP  | John Poul  | 2007-05-24  | 
|   2 | Learn MySQL | Abdul S   | 2007-05-24  | 
|   3 | JAVA Tutorial | Sanjay   | 2007-05-06  | 
+-------------+----------------+-----------------+-----------------+ 

SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count 
    -> FROM tutorials_tbl a, tcount_tbl b 
    -> WHERE a.tutorial_author = b.tutorial_author; 

這是我如何加入兩個表爲你的情況,你都沒有提到整個列名所以我只是假設,給你一個簡單的例子進行聯接。你可以做進一步的聯接像左連接等,從3個表 Source fetching from 3 tables

0
<table align="center" border="1" width="100%"> 
<tr> 
<th>Rank</th> 
<th>Name</th> 
<th>Kills</th> 
<th>Deaths</th> 
</tr> 

    $res=mysql_query("SELECT c.* , p.*,d.* FROM ps_plr c,ps_plr_ids_name p,ps_plr_victms d WHERE c.plrid=p.plrid and c.plrid = d.plrid "); 
    while($row=mysql_fetch_array($res)) 
    { 
    ?> 
     <tr> 
     <td><p><?php echo $row[' Rank']; ?></p></td> 
     <td><p><?php echo $row['Name']; ?></p></td> 
     <td><p><?php echo $row['Kills']; ?></p></td> 
     <td><p><?php echo $row['deaths']; ?></p></td> 
     </tr> 
     <?php 
    } 
    ?> 
</table> 

嘗試它希望這個作品來獲取..

相關問題