简体   繁体   English

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

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

I have a database named as ps_megasearch_attributes. 我有一个名为ps_megasearch_attributes的数据库。 For that my database query is like this 为此,我的数据库查询是这样的

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');

Now to get the values from the database I have this code in php 现在从数据库中获取值,我在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;
  }
}

?>

But this one is showing the return result array two times. 但这是两次显示返回结果数组。 So can someone kindly tell me how to do this? 那么有人可以告诉我该怎么做吗? Any help will be really appreciable. 任何帮助都是非常可观的。

mysqli_fetch_array mysqli_fetch_array

mysqli_result::fetch_array -- mysqli_fetch_array — Fetch a result row as an associative, a numeric array, or both mysqli_result :: fetch_array-mysqli_fetch_array —提取结果行作为关联数组,数字数组或两者

You don't require foreach loop. 您不需要foreach循环。

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

You have 2 loops there - the while and foreach. 您在那里有2个循环-while和foreach。 Try: 尝试:

<?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