Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何避免MySQL查询结果重复?_Php_Mysql - Fatal编程技术网

Php 如何避免MySQL查询结果重复?

Php 如何避免MySQL查询结果重复?,php,mysql,Php,Mysql,我有一个PHP脚本从MySQL查询中获取行,但是,一些serum会多次显示。我更希望每个血清只显示一次 <?php $typeno = 0; $categories = []; $gettestcat = mysqli_query($con,"SELECT * from types where testid = '$id'"); while($gtc = mysqli_fetch_array($gettestcat)){ $testid = $gtc['testtypeid'];

我有一个PHP脚本从MySQL查询中获取行,但是,一些
serum
会多次显示。我更希望每个
血清
只显示一次

<?php
$typeno = 0;
$categories = [];
$gettestcat = mysqli_query($con,"SELECT * from types where testid = '$id'");

while($gtc = mysqli_fetch_array($gettestcat)){
    $testid = $gtc['testtypeid'];
    $gettesttypename = mysqli_query($con,"SELECT * from tests where id = '$testid'");
    $gtyn = mysqli_fetch_array($gettesttypename);

    if (!in_array($gtyn['sample_type'], $categories)) {
        $categories [] = $gtyn['sample_type'];
    }
}

$countcat = count($categories);
$gettesttypes = mysqli_query($con,"SELECT * from types where testid = '$id'");

while($gtt = mysqli_fetch_array($gettesttypes)){
    $testid = $gtt['testtypeid'];
    $gettesttypenames = mysqli_query($con,"SELECT * from tests where id = '$testid'");
    $gtyns = mysqli_fetch_array($gettesttypenames);

    if ($gtyns['sample_type'] == 'Serum') {
        echo $gi['name'].' - '.$gtyns['test_name'].' - ';
    }
}
?>

<?php  ?>
<?php   
?>

您可以将输出的名称存储在数组中。然后,在回显该名称之前,检查该名称是否存在于数组中

// Initialize the array
$names = [];

while($gtt = mysqli_fetch_array($gettesttypes)){
    $testid = $gtt['testtypeid'];
    $gettesttypenames = mysqli_query($con,"SELECT * from tests where id = '$testid'");
    $gtyns = mysqli_fetch_array($gettesttypenames);

    if ($gtyns['sample_type'] == 'Serum') {

        if (!array_key_exists($gi['name'], $names)) {
            // The name doesn't exist in the array, echo it:
            echo $gi['name'] . '-';

            // Put the name into the array so it won't be echoed again
            $names[$gi['name']] = true;
        }

        echo $gtyns['test_name'].' - ';
    }
}

如果您使用mysql,请使用
DISTINCT
关键字获得唯一的结果

为什么要多次执行相同的查询,而不是重复使用结果?看起来很奇怪。不过,我没有深入研究,它非常确定可以通过相当简单的联接和/或分组方式(甚至只使用简单的select)来完成。警告:您对参数化预处理语句非常开放,应该使用参数化预处理语句,而不是手动构建查询。它们由或提供。永远不要相信任何类型的输入,尤其是来自客户端的输入。即使您的查询仅由受信任的用户执行。