我遇到的问题是当我回显或打印以下变量时,我收到的数据只是我表格中列出的最后一项业务的数据。
目前无论我在哪个列表中点击,我都会获得返回上一次业务的相同数据集。
正如您在下面的代码中所看到的,我将从点击的列表中传递business_name以在我的查询中使用,以查找相关的业务配置文件信息。
$business_name = mysql_real_escape_string($_GET['business_name']);
$query = "SELECT
business_id,
category,
years_recommended,
profile_size,
business_name,
established,
employees,
service,
strengths,
ideal_for,
reassurance
FROM
business_data
WHERE
business_name = '$business_name'
AND
profile_size = 'A'
OR
profile_size = 'B'
OR
profile_size = 'C'
OR
profile_size = 'D'
OR
profile_size = 'E'";
$result = mysql_query($query, $dbc)
or die (mysql_error($dbc));
while($row = mysql_fetch_array($result)) {
$business_id = $row["business_id"];
$profile_size = $row["profile_size"];
$category = $row["category"];
$years = $row["years_recommended"];
$established = $row["established"];
$employees = $row["employees"];
$service = $row["service"];
$strengths = $row["strengths"];
$ideal_for = $row["ideal_for"];
$reassurance = $row["reassurance"];
}
echo...
如果您需要更多信息,请告诉我们。
我的代码有什么问题吗?
非常感谢提前。
答案 0 :(得分:7)
您的echo
调用位于获取循环之外,因此即使返回其他结果,您也只会看到最后的结果。
while($row = mysql_fetch_array($result)) {
$business_id = $row["business_id"];
$profile_size = $row["profile_size"];
$category = $row["category"];
$years = $row["years_recommended"];
$established = $row["established"];
$employees = $row["employees"];
$service = $row["service"];
$strengths = $row["strengths"];
$ideal_for = $row["ideal_for"];
$reassurance = $row["reassurance"];
// Echo **inside** the loop
echo...
}
如果您愿意,可以将所有结果存储在一个大型数组中,然后可以根据需要多次在脚本中的任何位置使用:
// Array for all results
$results = array();
while($row = mysql_fetch_array($result)) {
// Append each row fetched onto the big array
$results[] = $row;
}
// Now use it as needed:
foreach ($results as $r) {
echo $r['profile_size'];
print_r($r);
}
答案 1 :(得分:1)
你的回音应该在循环中