我遇到的问题是当我回显或打印以下变量时,我收到的数据只是我表中列出的最后一个商家的数据.
目前无论我在哪个列表中单击,我都会获得返回的最后一个业务的相同数据集.
正如您在下面的代码中看到的那样,我将从单击的列表中传递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...
Run Code Online (Sandbox Code Playgroud)
如果您需要更多信息,请告诉我.
我的代码有什么问题吗?
提前谢谢了.
您的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...
}
Run Code Online (Sandbox Code Playgroud)
如果您愿意,可以将所有结果存储在一个大型数组中,然后可以根据需要多次在脚本中的任何位置使用:
// 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);
}
Run Code Online (Sandbox Code Playgroud)