0

我在mysql中有两个表

practice_sheetsparent_pin

我想使用一个选择语句并从两个表中获取数据。

我试过了

$result = mysqli_query($con,"SELECT * FROM practice_sheets AND parent_pin 
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");

并且:

$result = mysqli_query($con,"SELECT * FROM practice_sheets, parent_pin 
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");

我以前从未尝试过这样做,而以前的解决方案正是我所搜索的。

更新

我认为如果我包含我的完整代码会有所帮助。表格数据将进入我页面上的表格。将匹配student_name来自 practice_sheets 和parent_pin的字段。parents_student

<?php
$con=mysqli_connect();
// Check connection

if (mysqli_connect_errno()){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM practice_sheets
                                                    WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);

if($numrows == 0)                    {
    echo "<div class='alert alert-danger'>";
    echo "No Entries, See your instructor for details.";
    echo "</div>";
} else                    {
    echo "<table class='mws-table table-striped table-hover'>";
    echo "<thead align='center'>";
    echo "<tr>";
    echo "<th>Sheet Number</th>";
    echo "<th>Total Minutes</th>";
    echo "<th>Due Date</th>";
    echo "<th>PIN</th>";
    echo "<th>View</th>";
    echo "</tr>";
    echo "</thead>";
    echo "<tbody align='center'>";
    while($row = mysqli_fetch_array($result)){

        if ($row["total_min"]>=$row["required_min"]) {
            echo "<tr class='success'>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['total_min'] . "</td>";
            echo "<td>" . $row['due_date'] . "</td>";
            echo "<td>" . $row['parent_pin'] . "</td>";
            echo "<td> <a href='account/practiceSheets?id=" . $row["id"] . "&total_min=" . $row["total_min"] ."&due_date=" . $row["due_date"] ."&mon_min=" . $row["mon_min"] ."&tues_min=" . $row["tues_min"] ."&wed_min=" . $row["wed_min"] ."&thurs_min=" . $row["thurs_min"] ."&fri_min=" . $row["fri_min"] ."&sat_min=" . $row["sat_min"] ."&sun_min=" . $row["sun_min"] ."&name=" . $row["student_name"] ."&assignment=" . $row["assignment"] ."&required_min=" . $row["required_min"] ."'> <i class='icon-eye-open'> </i> </a> </td>";
            echo "</tr>";
        } else {
            echo "<tr class='info'>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['total_min'] . "</td>";
            echo "<td>" . $row['due_date'] . "</td>";
            echo "<td>" . $row['parent_pin'] . "</td>";
            echo "<td> <a href='account/practiceSheets?id=" . $row["id"] . "&total_min=" . $row["total_min"] ."&due_date=" . $row["due_date"] ."&mon_min=" . $row["mon_min"] ."&tues_min=" . $row["tues_min"] ."&wed_min=" . $row["wed_min"] ."&thurs_min=" . $row["thurs_min"] ."&fri_min=" . $row["fri_min"] ."&sat_min=" . $row["sat_min"] ."&sun_min=" . $row["sun_min"] ."&name=" . $row["student_name"] ."&assignment=" . $row["assignment"] ."&required_min=" . $row["required_min"] ."'> <i class='icon-eye-open'> </i> </a> </td>";
            echo "</tr>";
        }

    }

    echo "</tbody>";
    echo "</table>";
    mysqli_close($con);
}

?>
4

2 回答 2

2
$result = mysqli_query($con,"SELECT * 
                             FROM practice_sheets, parent_pin 
                             WHERE student_name = parents_student 
     AND student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
于 2013-07-29T20:00:01.787 回答
0

对 WHERE 语句使用显式名称,例如

$result = mysqli_query("SELECT student_name.practice_sheets FROM practice_sheets AND parent_pin WHERE student_name.practice_sheets = '{$_SESSION['SESS_FIRST_NAME']} {$_SESSION['SESS_LAST_NAME']}'");

MySQL 不会 AFAIK 自动检查约束在哪里并且是正确的,因此考虑到您可能有冲突的名称。请注意,这仍然是伪代码,您需要相应地更改获取的结果。通常,明确定义要获取的列也被认为是一种好习惯,但除此之外,您也可以使用 JOIN。

为了帮助编写更短的代码,您还可以对表名使用简写,例如

$result = mysqli_query("SELECT student_name.ps AS name, pin.pp AS pin FROM practice_sheets AS ps, parent_pin AS pp WHERE student_name.ps = '{$_SESSION['SESS_FIRST_NAME']} {$_SESSION['SESS_LAST_NAME']}'");

更新

您的更新版本中也有问题。您调用mysqli_fetch_array,它返回一个有序(即编号)数组。如果您想使用键控,请使用mysqli_fetch_assoc.

只有当查询成功时,您才会关闭 MySQL 连接。移到mysqli_close括号外。

于 2013-07-29T19:54:32.623 回答