drs3925 2017-03-17 01:34
浏览 146
已采纳

INNER JOIN后表中的字段未正确显示 - 未定义的索引

Well, I've been trying to deal with this problem the whole afternoon/evening, and I know I'm close... still can't get it right :/

I have a database (created with phpMyAdmin as I'm learning to use it), it has subjects and careers, and fields like id, name, description, etc.

Table Subjects has an id AND a foreign key from table Careers (the id for Careers). I've done that in phpMyAdmin using the relation view.

Then... I have a main php file for the main page (like a "home"), and in there I've included a link to another php file which shows the table with all the subjects (Name of the subject, Description, Hours, Career, and Actions)

This is the table part in the 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>

And this is the php file that deals with showing the subjects and informations from the database:

<!-- 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>";
}
?>

It actually works fine... except when trying to show the name of the SUBJECTS :/ In this part:

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 :/

I can't seem to understand WHY it displays all the data from SUBJECT and even the name of the CAREER but not the name of the SUBJECT :/ If I write name on it, it still displays the name of the career. If I write "subject.name", still says "Undefined index"

  • 写回答

2条回答 默认 最新

  • doufei4418 2017-03-17 01:40
    关注

    The table prefix isn't included in the keys of the associative array returned by fetch_assoc(), they just have the column names. So if you select both s.name and c.name, there will just be one $row['name'], which contains the last one in the SELECT list.

    To get both, you should give one of them an alias.

    $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)";
    

    Then you can use $row['name'] for s.name and $row['career_name'] for c.name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮