php 在 foreach 循环中使用 SQL 结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8538511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using an SQL result in a foreach loop
提问by Richie
I feel like I am missing something stupidly obvious here, I am trying to get the results of an SQL query and then using them in a loop. I feel like I am missing something stupidly obvious, I have tried it with and without the commented out line.
我觉得我在这里遗漏了一些非常明显的东西,我正在尝试获取 SQL 查询的结果,然后在循环中使用它们。我觉得我错过了一些非常明显的东西,我已经尝试过使用和不使用注释掉的行。
<?php
$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 "<div><p>" . $forename . " " . $surname . "</p>
<p>Subject: " . $subject ."</p>
<p>Body: " . $body . "</p></div>";
}
if ($usertype == "employer"){
$forname = $messageData['forename'];
$surname = $messageData['surname'];
$companyName = $messageData['companyName'];
echo "<div><p>" . $forename . " " . $surname . " - " . $companyName . "</p>
<p>Subject: " . $subject ."</p>
<p>Body: " . $body . "</p></div>";
}
}
?>
Any help would be greatly appreciated
任何帮助将不胜感激
回答by Michael Berkowski
You must first fetch your results into an array. Looks like you started to do this but commented it out.
您必须首先将结果提取到数组中。看起来您已经开始这样做了,但已将其注释掉。
$results = mysql_query($query);
//$userData = mysql_fetch_array($results, MYSQL_ASSOC);
$resultset = array();
while ($row = mysql_fetch_array($results)) {
$resultset[] = $row;
}
// $resultset now holds all rows from the first query.
foreach ($resultset as $result){
//... etc...
回答by Crontab
Instead of your foreach()
, you should do something like this (see the mysql_query() manual pagefor more):
而不是你的foreach()
,你应该做这样的事情(有关更多信息,请参阅mysql_query() 手册页):
while($result = mysql_fetch_assoc($results)) {
// your code
}
回答by matting
another option
另外一个选项
$num_rows = mysql_num_rows($result)
for ($i=0;$i<$num_rows;$i++) {
$row = mysql_fetch_assoc($result)
$messageID = $row['messageID'];
}
You can do anything from here.
你可以从这里做任何事情。
Remember, 1- query into object like $result 2- fetch row at a time from the object into an array which reflects an entire row in the table given your query definition with associative keys or numeric 3- do something with the array
请记住,1- 查询像 $result 这样的对象 2- 一次从对象中提取行到一个数组中,该数组反映表中的整行,给定您的查询定义与关联键或数字 3- 对数组进行处理
You will loop through the object row by row and put in $row as an array.
您将逐行遍历对象并将 $row 作为数组放入。
Cheers
干杯
回答by Bartho
I don't have the reputation to comment, and the above answers are correct, but the php mysql_query() manual pagesays that
我没有评论的声誉,上面的答案是正确的,但是php mysql_query() 手册页说
this extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used
这个扩展在 PHP 5.5.0 中被弃用,在 PHP 7.0.0 中被移除。相反,应使用 MySQLi 或 PDO_MySQL 扩展
So now a correct way would be:
所以现在正确的方法是:
while($result = mysqli_fetch_assoc($results)) {
// your code
}