我有一个名为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;
}
}
?>
但这一次显示返回结果数组两次。那么有人可以告诉我该怎么做吗?任何帮助都会非常明显。
答案 0 :(得分:4)
mysqli_result :: fetch_array - mysqli_fetch_array - 将结果行提取为关联行,数字数组或两者
您不需要foreach
循环。
while($attributes_list=mysqli_fetch_array($attribute_name_fetch )){
echo $attributes_list['attribute_name']
}
答案 1 :(得分: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;
}
?>