2014-01-22 90 views
1

我有一个名为ps_megasearch_attributes的数据库。对于我的数据库查询是这样MySQL返回的结果在while循环中显示两次?

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; 
    } 
} 

?> 

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

+0

不需要列名和表名的单引号,这可以让你查询问题。通常,单引号仅用于值。 – Yani

回答

4

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'] 
} 
+0

那么如何解决这个问题呢? – Jagdish

+0

是的,正在写解决方案。立即看到编辑。 – Rikesh

1

你有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; 
} 
?> 
相关问题