我的info
表格包含以下列:
Id | Name | City | date | status
我想从info
中选择所有名称,并使用以下查询:
$query = mysql_query("SELECT name FROM info WHERE status = 1 ORDER BY id")
or die(mysql_error());
while ($raw = mysql_fetch_array($query))
{
$name = $raw["name"];
echo ''.$name.'<br>';
}
命名列内容示例:
alex
alex1
alex2
michel22
michel33
michel44
好吧,结果是它返回所有条目。我希望回显所有条目而不重复但只有内容指定的关键字
说:在原始name
下,我们已经插入名称“alex and michel”10次,格式不同。
我只想回应:
alex
michel
有一次可能吗?
答案 0 :(得分:0)
首先使用mysqli或pdo来连接数据库。不推荐使用mysql。
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$result = mysqli_query($mysqli,"SELECT name FROM info WHERE status = 1 ORDER BY id") or die(mysqli_error($mysqli));
$uniqueNames = array();
while ($row = mysqli_fetch_assoc($result)) {
$nameWithoutNumbers = preg_replace('/[0-9]+/', '', $row["name"]); //removes the numbers and leave only names
$uniqueNames[$nameWithoutNumbers] = 1; //just a filtering hack, if key exits value is replaced otherwise a new key is created
}
foreach($uniqueNames as $name => $one)
echo $name; //remember you were saving your names as keys pointing to 1s