gpt4 book ai didi

PHP 从 mysqli_fetch_row 数组中排序结果

转载 作者:行者123 更新时间:2023-11-29 05:23:57 25 4
gpt4 key购买 nike

我有一个简单的查询来从一个表中提取所有数据,但我只需要显示一次该值,无论它在表中出现多少次。现在我得到以下结果:

Material in Que
9231500
9231500
9231500
Grp1298
Grp1298
6251752
6251752

我想要的结果是:

Material in Que
9231500
6251752
Grp1298

如何从查询结果中整理出重复的数字或文本?对于我表中第 3 列的数组 $row[2]

<?php session_start(); ?>  
<html>
<head>
<basefont face="Arial">
<title>Material in Testing que</title>
</head>

<body>
<?php

// set database server access variables:
include('db.php');

// open connection
$connection = mysqli_connect($host, $user, $pass, $db);
if (mysqli_connect_errno($connection)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// create query
$query = "SELECT * FROM testingqa1160";
$result = mysqli_query($connection, $query);

if (mysqli_num_rows($result) > 0) {
echo "Material in Testing Que";
echo "<br>";

while($row = mysqli_fetch_row($result)) {
echo $row[2];
echo "<br>";
echo " ";
}
}
else {
// print status message
echo "<center>";
echo " Que Empty </font>";
echo "</center>";
}

mysqli_free_result($result);

// close connection
mysqli_close($connection);

?>
</body>
</html>

最佳答案

在查询中使用 MySQL 的 DISTINCT:

$query = "SELECT DISTINCT colname FROM testingqa1160";

关于PHP 从 mysqli_fetch_row 数组中排序结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22413897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com