php mysql查询返回许多数组但只需要一个

时间:2017-03-29 02:26:16

标签: php mysql arrays

$connection = db_connect();
$query = mysqli_query($connection, "
SELECT * FROM birouri 
WHERE disponibilitate = 'LIBER' 
AND locatie_actuala = 'Orhideea' 
AND pauza = '' 
AND closed_program = '' 
AND feedback = '' 
LIMIT 1") or die(mysqli_error($connection));
            $row = mysqli_fetch_assoc($query);

            $username = $row['username'];
            $nr_birou = $row['nr_birou'];
            $disponibilitate = $row['disponibilitate'];
            $locatie_actuala = $row['locatie_actuala'];
            $pauza = $row['pauza'];
            $closed_program = $row['closed_program'];
            $feedback = $row['feedback'];
            $inregistrare_clienti = $row['inregistrare_clienti'];

            var_dump($row);

在数据库中,我有6行符合我的查询条件。

var_dump($row);返回1个数组(第一行)重复7次,但我只需要显示一个。 Limit 1无效。

请帮助。

1 个答案:

答案 0 :(得分:0)

更改此

$row = mysqli_fetch_assoc($query);

            $username = $row['username'];
            $nr_birou = $row['nr_birou'];
            $disponibilitate = $row['disponibilitate'];
            $locatie_actuala = $row['locatie_actuala'];
            $pauza = $row['pauza'];
            $closed_program = $row['closed_program'];
            $feedback = $row['feedback'];
            $inregistrare_clienti = $row['inregistrare_clienti'];

到此,它将起作用

while($row = mysqli_fetch_assoc($query)){

            $username = $row['username'];
            $nr_birou = $row['nr_birou'];
            $disponibilitate = $row['disponibilitate'];
            $locatie_actuala = $row['locatie_actuala'];
            $pauza = $row['pauza'];
            $closed_program = $row['closed_program'];
            $feedback = $row['feedback'];
            $inregistrare_clienti = $row['inregistrare_clienti'];
}

有8列,所以你的代码运行8次,但如果运行while循环它只运行一次