繁体   English   中英

MySQL返回的结果在while循环中显示两次?

[英]MySQL returning result showing twice in while loop?

我有一个名为ps_megasearch_attributes的数据库。 为此,我的数据库查询是这样的

CREATE TABLE IF NOT EXISTS `ps_megasearch_attributes` (
  `attribute_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `attribute_name` text NOT NULL,
  PRIMARY KEY (`attribute_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8  ;

--
-- Dumping data for table `ps_megasearch_attributes`
--

INSERT INTO `ps_megasearch_attributes` (`attribute_id`, `attribute_name`) VALUES
(1, 'Color');

现在从数据库中获取值,我在PHP中有此代码

<?php 
$attribute_name_fetch = mysqli_query($con,"SELECT `attribute_name` FROM `ps_megasearch_attributes` ORDER BY `attribute_id` DESC LIMIT 1 " );
while($attributes_list=mysqli_fetch_array($attribute_name_fetch )){
  foreach($attributes_list as $attribute_name) {
    echo $attribute_name;
  }
}

?>

但这是两次显示返回结果数组。 那么有人可以告诉我该怎么做吗? 任何帮助都是非常可观的。

mysqli_fetch_array

mysqli_result :: fetch_array-mysqli_fetch_array —提取结果行作为关联数组,数字数组或两者

您不需要foreach循环。

while($attributes_list=mysqli_fetch_array($attribute_name_fetch )){
    echo $attributes_list['attribute_name']
}

您在那里有2个循环-while和foreach。 尝试:

<?php 
$attribute_name_fetch = mysqli_query($con,"SELECT `attribute_name` FROM `ps_megasearch_attributes` ORDER BY `attribute_id` DESC LIMIT 1 " );
$attributes_list=mysqli_fetch_array($attribute_name_fetch);
foreach($attributes_list as $attribute_name) {
  echo $attribute_name;
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM