选择和显示来自 '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,但我愿意学习。我昨天一整天都在浏览这个网站,但我没有遇到任何以前的帖子来回答我的问题。感谢您为像我这样的新手提供的任何帮助。
我目前的工作:
- 我已经在另一个会话中保存了用户的登录SESSION。它工作正常并根据用户名登录检索正确的用户行。
$_SESSION['appointment']
显示具有匹配用户 ID 的特定用户的约会数组
问题
$_SESSION['appointment']
仅显示具有匹配用户 ID 的特定用户的约会数组的第一个结果。
- 在我的例子中,它只显示 Bob Marley 的
$_SESSION['appointment']['date']
= 2016-12-20
而不会显示他的所有其他约会 2016-12-31
和 2016-12-29
即使它有相同匹配userID = 3
如果用户在 '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函数中的所有内容然后你应该使用
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;
}
我对使用 PHP 使用 MySQLi 进行编码非常陌生。我目前正在尝试编写一个简单的约会用户应用程序,在我的数据库中有 2 tables,我已经在其中输入了一些测试值,如下所示。
我真的尽我最大的努力,但我还不是很了解 MySQLi,但我愿意学习。我昨天一整天都在浏览这个网站,但我没有遇到任何以前的帖子来回答我的问题。感谢您为像我这样的新手提供的任何帮助。
我目前的工作:
- 我已经在另一个会话中保存了用户的登录SESSION。它工作正常并根据用户名登录检索正确的用户行。
$_SESSION['appointment']
显示具有匹配用户 ID 的特定用户的约会数组
问题
$_SESSION['appointment']
仅显示具有匹配用户 ID 的特定用户的约会数组的第一个结果。- 在我的例子中,它只显示 Bob Marley 的
$_SESSION['appointment']['date']
=2016-12-20
而不会显示他的所有其他约会2016-12-31
和2016-12-29
即使它有相同匹配userID = 3
如果用户在 '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函数中的所有内容然后你应该使用
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;
}