三元运算符

时间:2017-06-09 04:08:28

标签: php mysql pdo

我正在使用

检查用户信息是否在数据库中
$usernameQuery->rowCount();

但是,我也把这个代码放在一个三元运算符中并回显结果,如果它在那里是" false,如果它不是&#34 ;,则在json中为真,但是它不起作用。我研究了它,我发现的信息没有帮助,也没有三元运算符。这是代码

PHP

<?php

if($_SERVER['REQUEST_METHOD'] =="POST"){

    $email = $_POST['email'];
    $username = $_POST['username'];

    //if email and username is empty then do this
    if(!empty($email) && !empty($username)){

        //Connect to database
        try{

        // new php data object
        $handler = new PDO('mysql:host=127.0.0.1;dbname=magicsever', 'root', '');
        //ATTR_ERRMODE set to exception
        $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    }catch(PDOException $e){
        die("There was an error connecting to the database");   

    }

        //find email and username and Prepare
        $emailQuery = $handler->prepare( "SELECT * FROM app_signup WHERE email = ?");
        $usernameQuery = $handler->prepare("SELECT * FROM app_signup WHERE username = ?");

        //Execute;
        $emailQuery->execute(array($email));
        $usernameQuery->execute(array($username));

        //Check if the email is empty
        if(!empty($email)){

            echo "Test";
        }else{

            $nullResult = array();
            $nullResult['Email'] = (!empty($email))? "empty" : "";
            $nullResult['Username'] = ($usernameQuery->rowCount())? false: true;
            echo json_encode($nullResult);
            die("");
        }

    }else{

        $nullResult = array();
        $nullResult['Email'] = (empty($email))? "empty" : "";
        $nullResult['Username'] = (empty($username))? "empty" : "";
        echo json_encode($nullResult);
        exit();

    }



}





?>

2 个答案:

答案 0 :(得分:2)

试试这个,我想你没有检查条件

$count = $usernameQuery->rowCount();

$nullResult['Username'] = (!empty($count)) ? true: false;

阅读你的问题之后还有一件事也是错的,你在同一个表中使用不同的查询检查两列,我认为如果它不需要单独作为你的要求就应该是这样的

$data = $handler->prepare( "SELECT * FROM app_signup WHERE email = ? OR username = ?");

$data>execute(array($email,$username));

然后检查

$nullResult = array();
$nullResult['Email'] = (!empty($data['email'])) ? $data['email'] : 'empty'; 
$nullResult['Username'] = (!empty($data->rowCount()) ? false : true;

这是基于您在问题中提供的详细信息的解决方案,希望这有帮助

<?php

if($_SERVER['REQUEST_METHOD'] =="POST"){

    $email = $_POST['email'];
    $username = $_POST['username'];

    //if email and username is empty then do this
    if(!empty($email) && !empty($username)){

        //Connect to database
        try{

        // new php data object
        $handler = new PDO('mysql:host=127.0.0.1;dbname=magicsever', 'root', '');
        //ATTR_ERRMODE set to exception
        $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    }catch(PDOException $e){
        die("There was an error connecting to the database");   

    }

        //find email and username and Prepare
        $dataQuery = $handler->prepare( "SELECT * FROM app_signup WHERE email = ? OR username = ?");

        //Execute;
        $dataQuery->execute(array($email,$user));

        $count = $dataQuery->rowCount()
        //Check if the email is empty
        if(!empty($count)){
                echo "Test";
        }else{

            $nullResult = array();
            $nullResult['Email'] = (!empty($data['email'])) ? $data['email'] : 'empty'; 
            $nullResult['Username'] = (!empty($data->rowCount()) ? false : true;
            echo json_encode($nullResult);
            die("");
        }

    }else{

        $nullResult = array();
        $nullResult['Email'] = (empty($data['email']))? "empty" : "";
        $nullResult['Username'] = (empty($data['username']))? "empty" : "";
        echo json_encode($nullResult);
        exit();

    }



}

答案 1 :(得分:0)

我实际上通过改变一些东西来修复它。问题是if和else语句,我有&#34;如果电子邮件和密码不是空的&#34;然后运行代码。但我正在检查if和else语句,如果用户名是好的,并且电子邮件是空的,那么第一部分变为false并运行按钮代码。这是代码

http {
  ...

  upstream shopster-node {
    server node:5000 weight=10 max_fails=3 fail_timeout=30s;
    server node:5500 weight=10 max_fails=3 fail_timeout=30s;
    keepalive 64;
  }

  server {
    ...
  }

}