Loop results PDO PHP

前端 未结 2 400
小鲜肉
小鲜肉 2021-01-06 15:03

I\'m trying to fetch all records from my table on my site, I have the following

$sth = $conn->prepare(\"SELECT * FROM directory WHERE user_active != \'\'         


        
2条回答
  •  囚心锁ツ
    2021-01-06 15:45

    You need to use a loop:

    while ($result = $sth->fetch(PDO::FETCH_ASSOC)) {
        echo $result[First_Name];
        echo ' ' . $result[Surname];
    }
    

    Or you could use fetchAll method:

    foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $result) {
        echo $result[First_Name];
        echo ' ' . $result[Surname];
    }
    

    And Note: If First_Name and Surname are not constants, then you should use the string as the key name.

    $result['First_Name'] and $result['Surname']

提交回复
热议问题