我觉得我错过了一些愚蠢明显的东西,我试图获得SQL查询的结果,然后在循环中使用它们.我觉得我错过了一些愚蠢明显的东西,我已经尝试过,有没有注释掉的线.
$sentToID = $_SESSION['userID'];
$query = "SELECT *
FROM messages
WHERE sentToID = '$sentToID'";
$results = mysql_query($query);
//$userData = mysql_fetch_array($results, MYSQL_ASSOC);
foreach ($results as $result){
$messageID = $result['messageID'];
$sentFromID = $result['sentFromID'];
$subject = $result['subject'];
$body = $result['body'];
$dateTime = $result['dateTime'];
$query = "SELECT usertype
FROM user
WHERE userID = '$sentFromID'";
$messageResult = mysql_query($query);
$messageData = mysql_fetch_array($messageResult, MYSQL_ASSOC);
$usertype = $messageData['usertype'];
$query = "SELECT *
FROM $usertype
WHERE userID = '$sentFromID'";
$messageResult = mysql_query($query);
$messageData = mysql_fetch_array($messageResult, MYSQL_ASSOC);
if ($usertype == "jobseeker"){
$forname = $messageData['forename'];
$surname = $messageData['surname'];
echo "
" . $forename . " " . $surname . "
Subject: " . $subject ."
Body: " . $body . "
}
if ($usertype == "employer"){
$forname = $messageData['forename'];
$surname = $messageData['surname'];
$companyName = $messageData['companyName'];
echo "
" . $forename . " " . $surname . " - " . $companyName . "
Subject: " . $subject ."
Body: " . $body . "
}
}
?>
任何帮助将不胜感激
解决方法:
而不是你的foreach(),你应该做这样的事情(更多信息见mysql_query() manual page):
while($result = mysql_fetch_assoc($results)) {
// your code
}
标签:php,mysql,foreach
来源: https://codeday.me/bug/20191006/1862913.html