php - 为什么我的 else 语句运行代码两次

标签 php mysql database if-statement pdo

我的代码:

  <?php
    $name = $_POST["name"];
//establishing connection through PDO
    require("database.php");

    try{
     //querying the firstname data from the people table    
        $sql = $conn->query("SELECT firstname FROM people");

    }catch (Exception $e) {
            echo "Data could not be retrieved from the database.";
            exit;
        }

//looping through the array of firstname and echoing it out if it equals to the user input of name. else just echo out sorry no match 
    while($theRow = $sql->fetch(PDO::FETCH_ASSOC)){
    if(strtolower( $name ) == strtolower( $theRow["firstname"] ) && isset($_POST['name']) ){
    echo $theRow['firstname'] . "<br />";
    }else {
        echo 'Sorry no match';
        exit;
    }
    }
    ?>

require database.php 只是使用 PDO 建立与我的数据库的连接。

我的数据库中只有 2 行

'firstname' of

  • jack
  • 鲍勃

如果有人在我的输入字段中键入这两个名称之一,php 将从数据库中的人员表中回显该名称。非常简单,但我遇到的唯一问题是在我的 else 语句中,如果名称的输入字段不等于数据库中的任何名称,我希望它回显抱歉,不匹配。但相反,它回显了抱歉,每个名字都没有匹配一次。我知道我正在遍历数据库名称数组,但我只希望它在输入的名称不等于数据库中的“名字”时回显抱歉,不匹配一次。

额外注意:

我也尝试过使用 foreach 循环而不是使用 fetchAll 方法的 while 循环,而不只是 fetch 但没有运气。基本上给了我相同的结果。

问题更新:

When I load the page the else statement is already taking effect and echoing out Sorry no match even before I set a name in the input. and if I type the wrong name it ill echo out Sorry no match twice if I type the correct name it will echo out the name out of the database and Sorry no match once.

搞清楚了:

<?php
$name = $_POST["name"];
require("database.php");

try{
    $sql = $conn->prepare("SELECT firstname FROM people WHERE firstname = ?");
    $sql->bindParam(1,$name);
    $sql->execute();

}catch (Exception $e) {
        echo "Data could not be retrieved from the database.";
        exit;
    }

$theRow = $sql->fetch(PDO::FETCH_ASSOC);

if(strtolower( $name ) == strtolower( $theRow["firstname"] ) ){
echo $theRow['firstname'] . "<br />";
}else{
    echo 'no match'; 
}
?>

事实证明,我什至不需要对 WHERE 子句进行循环,只获取与 $_POST['name'] 匹配的名字,所以这只是何时输出该数据的问题,这就是 if 语句的时间进来了。但是如果我必须输出多个数据,我可能会像这样使用 foreach 循环:

if(strtolower( $name ) == strtolower( $theRow["firstname"] ) ){
foreach( $theRow as $row ){
    echo $row . "<br />";
    }
    }else{
        echo 'no match'; 
    }

如果有人发现此代码或我的方法有任何问题,请告诉我。谢谢

最佳答案

首先,您似乎回答了自己的问题:为什么 else 语句运行代码两次?答:不是;它在循环的每次迭代中运行一次,因为您将它放入循环中。

只需将您的 SQL 更改为:

$stmt = $conn->prepare("SELECT firstname FROM people where firstname = ?");
$stmt->execute(array($name));
$result = $stmt->fetchAll();

它会返回 1 行或 0 行。所以您的 if 语句将如下所示:

if($result->num_rows) // True if num_rows > 0; else false

并将您的while 循环放在您的if 语句中。保持您的 else 语句只是echo 'Sorry no match';

关于php - 为什么我的 else 语句运行代码两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29117677/

相关文章:

javascript - 使用ajax为每个循环刷新div,结果错误

php - magento 存储国家/地区全名的位置

php - $query->row() 和 $query->result() 最简单的解释

mysql - 如何计算两个日期之间一个月内的叶子数?

python - Web 应用程序中高效的作业进度更新

php - unicode中字符串的长度不同

PHP 图表库

mysql - 大文本字段如何存储在 mySQL 中?

database - 用于 MSISDN 存储的最佳数据类型和长度

mysql - 3张表之间的关系