簡體   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