douhui3760 2015-03-18 08:58
浏览 16
已采纳

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

My code:

  <?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;
    }
    }
    ?>

the require database.php is just establishing connection to my database using PDO.

I just have 2 rows in my database with

'firstname' of

  • Jack
  • Bob

and if in my input field anyone types one of those 2 names php will echo out that name from the people table in the database. Very simple but the only problem I am having is on my else statement I wanted it to echo out Sorry no match if the input field of name is not equal to any name in the database. BUT instead it echo's out Sorry no match once for each name. I understand that I am looping through the array of database name but I only want it to echo Sorry no match once if the name input is not equal to a "firstname" in the database.

EXTRA NOTE:

I have also tried using a foreach loop instead of the while looping with the fetchAll method instead of just fetch but no luck there. Basically gave me the same results.

UPDATE ON THE PROBLEM:

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.

FIGURED IT OUT:

<?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'; 
}
?>

Turns out I did not even need a loop do to the WHERE claus only getting the firstname that matched $_POST['name'] so it was just a matter of when to out put that data and that was when the if statement came in. But if I had to output more than one single data I would of probably used a foreach loop like so:

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

If anyone sees any problem with this code or my method please do let me know. Thank you

  • 写回答

1条回答 默认 最新

  • douyan1244 2015-03-18 09:05
    关注

    Firstly, you seem to answer your own question: Why is the else statement running the code twice? Ans: it's not; it's running it once for each iteration of the loop, because you put it in the loop.

    Just change your SQL to:

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

    And it'll either return 1 or 0 rows. So your if statement will look like this:

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

    And put your while loop inside your if statement. Keep your else statement to just echo 'Sorry no match';.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算