检查结果后,显示语句结果不起作用

时间:2022-09-22 17:08:13

I've wrote a code to select data from my database, and I test if the statement returns something or no with fetchColumn() and I display the results if it's not null using a loop with fetch(). But event when the results exist it soesn't show me anything.. Here's my code :

我已经编写了一个代码来从我的数据库中选择数据,并且我使用fetchColumn()来测试语句是否返回了某些内容,并且如果使用带有fetch()的循环它不是null,则显示结果。但是当结果存在的事件时它并没有向我显示任何东西..这是我的代码:

$req = $bdd->prepare('SELECT NOM_Etudiant, PRENOM_Etudiant FROM Etudiant WHERE CNE_Etudiant = :cnev AND PASS_Etudiant = :passv');
$req->execute(array('cnev' => $cne, 'passv' => $pass));

$count = $req->fetchColumn();
if(!$count) {
    header('Location: authentification_etud.php?status=invalid');
}
else {
    // Doesn't work
    while ($donnees = $req->fetch())
    {
        echo '<strong>Bienvenue </strong>' . $donnees['NOM_Etudiant'] . ' ' . $donnees['PRENOM_Etudiant'] . ' ! ' ;
    }
}

Do you know why it doesn't work ? Thank you :)

你知道为什么它不起作用吗?谢谢 :)

2 个解决方案

#1


0  

You should use a rowCount() method to determine the number of rows not fetchColumn(). fetchColumn() will actually advance the pointer in the result set to where you will no longer have access to the first row of data.

您应该使用rowCount()方法来确定不是fetchColumn()的行数。 fetchColumn()实际上会将结果集中的指针前进到您将无法再访问第一行数据的位置。

Notice the big red warning on this page http://php.net/manual/en/pdostatement.fetchcolumn.php

请注意此页面上的大红色警告http://php.net/manual/en/pdostatement.fetchcolumn.php

#2


0  

fetchColumn returns one value of the first column and moves the result pointer to the next dataset. So, when calling fetch(), you don't get the first, but the second dataset (or nothing, if there is only one row).

fetchColumn返回第一列的一个值,并将结果指针移动到下一个数据集。因此,在调用fetch()时,您不会获得第一个数据集,而是第二个数据集(如果只有一行,则不会获取)。

Instead of using fetchColumn() you could just use rowCount() to check if there are results.

您可以使用rowCount()来检查是否有结果,而不是使用fetchColumn()。

#1


0  

You should use a rowCount() method to determine the number of rows not fetchColumn(). fetchColumn() will actually advance the pointer in the result set to where you will no longer have access to the first row of data.

您应该使用rowCount()方法来确定不是fetchColumn()的行数。 fetchColumn()实际上会将结果集中的指针前进到您将无法再访问第一行数据的位置。

Notice the big red warning on this page http://php.net/manual/en/pdostatement.fetchcolumn.php

请注意此页面上的大红色警告http://php.net/manual/en/pdostatement.fetchcolumn.php

#2


0  

fetchColumn returns one value of the first column and moves the result pointer to the next dataset. So, when calling fetch(), you don't get the first, but the second dataset (or nothing, if there is only one row).

fetchColumn返回第一列的一个值,并将结果指针移动到下一个数据集。因此,在调用fetch()时,您不会获得第一个数据集,而是第二个数据集(如果只有一行,则不会获取)。

Instead of using fetchColumn() you could just use rowCount() to check if there are results.

您可以使用rowCount()来检查是否有结果,而不是使用fetchColumn()。