PHP for循环中的sql语句,php – 在foreach循环中使用SQL结果

我觉得我错过了一些愚蠢明显的东西,我试图获得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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值