2017-03-17 34 views
1

嗯,我一直試圖在整個下午/晚上處理這個問題,並且我知道我已經接近...仍然無法正確解決問題:/INNER JOIN後表格中的字段顯示不正確 - 未定義索引

我有一個數據庫(因爲我學習使用它與phpMyAdmin創建),它具有學科和職業,而像ID,名稱,描述等領域

表科目都有一個id和來自表Careers(職業編號)的外鍵。我已經做了在phpMyAdmin使用的關係圖。

然後......我對主頁的主PHP文件(如「家」),並在那裏我已經包括到另一個PHP文件的鏈接,顯示了所有科目的表(名稱主題,描述,時間,職業,和動作)

這是Home.php表部分:

<table class="table table-striped table-bordered table-hover tabla"> 
       <thead> 
        <tr> 
         <th class="thead_texto">Subject</th> 
         <th class="thead_texto">Description</th> 
         <th class="thead_texto">Hours</th> 
         <th class="thead_texto">Carrer</th> 
         <th class="thead_texto">Actions</th> 
        </tr> 
       </thead> 
       <tbody><!-- Loop for the subjects --> 
        <?php 
         include("../extras/tablaSubjects.php"); 
        ?> 
       </tbody> 
</table> 

這是PHP文件,顯示與由受試者和交易信息數據庫:

<!-- This comes from another file that have the info for the conection --> 
<?php 
    include("conexion.php"); 

//Conection 
$CONN = new mysqli($serverName, $username, $password, $dataBase); 

//Checking 
if ($CONN->connect_error){ 
    echo("Error de conexion"); 
} 
else{ 
    echo "Conectado"; 
} 

//Showing the subjects and information 

//Query to select 
$SQL = "SELECT s.id, s.name, s.description, s.hours, c.name FROM subjects m JOIN careers c ON (s.career_id = c.id)"; 
$resultado = $CONN->query($SQL); 

//Part of the tbody where the loop will be 
if($result->num_rows > 0){ 
while($row = $result->fetch_assoc()){ 
    echo "<tr>"; 
     echo "<td>{$row['s.name']}</td>"; 
     echo "<td>{$row['description']}</td>"; 
     echo "<td>{$row['hours']}</td>"; 
     echo "<td>{$row['name']}</td>"; 
     echo '<td><a class="btn btn-primary" value="'.$row['id'].'">Edit</a> <a class="btn btn-danger" value="'.$row['id'].'">Delete</a> </td>'; 
    echo "</tr>"; 
} 
}else{ 
echo "<tr><td>No data<td></tr>"; 
} 
?> 

它實際上工作正常,試着展現對象的名稱時除外:/ 在這一部分:

echo "<td>{$row['s.name']}</td>"; --> this gives an error, "Undefined index" 
     echo "<td>{$row['description']}</td>"; --> OK, info from SUBJECT 
     echo "<td>{$row['hours']}</td>"; --> OK, info from SUBJECT 
     echo "<td>{$row['name']}</td>"; --> This displays the name of the CAREER :/ 

我似乎無法理解爲什麼它顯示從主題和甚至所有數據職業的名稱,但不是主題的名稱:/ 如果我寫它的名字,它仍然顯示了職業生涯的名稱。 如果我寫「subject.name」,仍然說「未定義索引」

+0

這與PhpMyAdmin應用程序有什麼關係?你有你自己的PHP腳本,你沒有使用該工具。 – Barmar

+0

難道不應該是'科目s',而不是'科目M'? – Barmar

+0

@Barmar嗨!是的,事情是我翻譯了整個事情,讓人們理解它哈哈我實際上使用西班牙語,所以「主題」是「materias」,所以使用m。我忘了在這裏改變它xD – xragdollqueen

回答

0

表前綴不包含在由fetch_assoc()返回的關聯數組的鍵中,它們只有列名。因此,如果同時選擇s.namec.name,則只會有一個$row['name'],其中包含SELECT列表中的最後一個。

要得到這兩個,你應該給他們中的一個別名。

$SQL = "SELECT s.id, s.name, s.description, s.hours, c.name AS career_name FROM subjects s JOIN careers c ON (s.career_id = c.id)"; 

然後你可以使用$row['name']s.name$row['career_name']c.name

+0

嗨!謝謝!這工作就像一個魅力! :D我知道我很近哈哈哈!我投了這個票,但不會公開更改,因爲我沒有足夠的聲望。我也接受了這個答案,因爲它是我用過的方法:D – xragdollqueen

0

您在您的select中有兩個name s。丟棄之前的週期和表名。所以,做這樣的事情:

SELECT s.id, s.name as s_name, s.description, s.hours, c.name 

然後你可以看一下s_name在結果集中,而不是s.name

如果你真的想s.name,你能做到這一點,以及:

SELECT s.id, s.name as `s.name`, s.description, s.hours, c.name 

請注意,您所需要的反引號,因爲時間還沒有標識符通常不允許的。

+0

@ gordon-lindoff嗨!感謝這個,我已經投了這個答案,但不會公開更改,因爲我沒有足夠的聲譽。 – xragdollqueen