Mysqli多查询或更好的选择

时间:2015-09-18 18:32:52

标签: php mysql

我有一个约会数据库,我可以成功打印数据。我现在要做的是限制显示给查询1的结果的约会。

我目前得到两个错误,第一个是意外查询,另一个错误是致命错误:函数名必须是字符串。我如何重写它以便两个查询都有效?或另一种更好的方法来实现这个目标

    <?php
    //MySqli Databse Connection
        require "id.php";
        require "calendarconnect.php";

    //MySqli Select Query 1
    $lep = $mysqli->query("SELECT lep FROM appointments WHERE ID = $contactid");
    $line = mysqli_fetch_array($query1, MYSQL_ASSOC);

    //MySqli Select Query 2
    $query = $mysqli->query("SELECT ID, appointment, nature, doctorname, lep FROM appointments WHERE lep = $line ORDER BY appointment");

    print '<table cellpadding="10" cellspacing="3" border="1" class="sortable">';
    print '<tr><th>Date/Time</th><th>Nature</th><th>Doctor Name</th><th>Actions</th></tr>';
    while($row = $query->fetch_assoc()) {
        print '<tr>';
        print "<td>" . date('m-d-y g:i A', strtotime($row['appointment'])) . "</td>";
        print '<td>'.$row["nature"].'</td>';
        print '<td>'.$row["doctorname"].'</td>';
        print "<td><a href=\"edit.php&cd=" . $row['ID'] . "\">Edit&nbsp;|&nbsp;</a>";
print "<a href=\"appointmentdelete.php?id=" . $row['ID'] . "\"onclick=
      \"return confirm('Are you sure you want to delete?')\">Delete</a></td>";
        print '</tr>';
    }  
    print '</table>';

    require "freeclose.php";
?>

1 个答案:

答案 0 :(得分:1)

没有必要使用2个Querys。因此,您不必将结果从查询1传输到客户端以放入第二个。

试试这个:

  SELECT b.ID, b.appointment, b.nature, b.doctorname, b.lep  
    FROM appointments a  
    LEFT JOIN appointments b ON b.lep = a.lep 
    WHERE a.ID = $contactid
    ORDER BY b.appointment;