选择和显示来自 'Appointment' table 的所有值以及来自 'User' table 的匹配用户 ID 的最佳做法是什么?

What is the best practice for selecting and displaying all values from an 'Appointment' table with a matching userID from a 'User' table?

我对使用 PHP 使用 MySQLi 进行编码非常陌生。我目前正在尝试编写一个简单的约会用户应用程序,在我的数据库中有 2 tables,我已经在其中输入了一些测试值,如下所示。

我真的尽我最大的努力,但我还不是很了解 MySQLi,但我愿意学习。我昨天一整天都在浏览这个网站,但我没有遇到任何以前的帖子来回答我的问题。感谢您为像我这样的新手提供的任何帮助。

我目前的工作:

问题

如果用户在 'appointment' table 中有匹配的 userID,我如何显示他们的所有相关约会日期?

我的数据库tables:

'users' table:

userID (column 1) // username (column 2) // name (column 3)

1 // user1 // Bob Dylan

2 // user2 // John Lennon

3 // user3 // Bob Marley

-

'appointment' table

'appointmentID' (column 1) // userID (column 2) // date (column 3)

1 (appointmentID) // 1 (userID) // 2016-12-07

2 (appointmentID) // 2 (userID) // 2016-12-15

3 (appointmentID) // 3 (userID) // 2016-12-20

4 (appointmentID) // 3 (userID) // 2016-12-31

5 (appointmentID) // 3 (userID) // 2016-12-29

我的课程:

 //Displays the array of the user
 $_SESSION['profile'] = displayProfile($_SESSION['user']);

 //Displays the array of the appointment based on the matching userID
 $_SESSION['appointment'] = displayAppointment($_SESSION['profile']['userID']);

我的 mySQLi 函数:

 function displayProfile($username)
 {
     global $ db_con; //my database connection

     $query = 'SELECT * FROM users WHERE username=\''.mysqli_escape_string($db_con, $username).'\'';
     $result = $db_con-> query($query);

     //This fetches the associative array for the user row 
     //(i.e. $_SESSION['profile']['userID'] outputs the userID of the user logged into the system
     if ($result->num_rows) { return $result->fetch_assoc();}
 }

  //////////////////////////////

   function displayAppointment($userID)
 {
     global $ db_con; //my database connection

     //This passes the current logged on user's userID into the query.
     //For example, if Bob Marley was currently logged on, the $userID that will be passed is userID = 3.
     $query = 'SELECT * FROM appointment WHERE userID='.$userID;
     $result = $db_con-> query($query);

     //This fetches the associative array for the appointment row with the correct matching userID
     //(i.e. $_SESSION['profile']['userID'] for Bob Marley is '3'
     if ($result->num_rows) { return $result->fetch_assoc();}
 }

所以你需要的是return函数中的所有内容然后你应该使用

fetch_all()

editenote:因为他没有 fetch_all

的驱动程序
function displayAppointment($userID)
 {
     global $ db_con; //my database connection

     //This passes the current logged on user's userID into the query.
     //For example, if Bob Marley was currently logged on, the $userID that will be passed is userID = 3.
     $query = 'SELECT * FROM appointment WHERE userID='.$userID;
     $result = $db_con-> query($query);

     //This fetches the associative array for the appointment row with the correct matching userID
     //(i.e. $_SESSION['profile']['userID'] for Bob Marley is '3' 
    $resultout=array();
    while ($row = mysqli_fetch_assoc($result)) {
        array_push($resultout, $row);
    }
    return $resultout;
 }