Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 三值运算符中的PDO json_编码_Php_Mysql_Pdo - Fatal编程技术网

Php 三值运算符中的PDO json_编码

Php 三值运算符中的PDO json_编码,php,mysql,pdo,Php,Mysql,Pdo,我正在使用检查数据库中是否有用户信息 $usernameQuery->rowCount(); 但是,我也将这段代码放在一个三元运算符中,并在json中回显结果,即“如果存在则为false,如果不存在则为true”,但它不起作用。我研究了一下,发现的信息没有帮助,也没有三元运算符。这是密码 PHP 试试这个,我想你没有检查条件 $count = $usernameQuery->rowCount(); $nullResult['Username'] = (!empty($count)

我正在使用检查数据库中是否有用户信息

$usernameQuery->rowCount();
但是,我也将这段代码放在一个三元运算符中,并在json中回显结果,即“如果存在则为false,如果不存在则为true”,但它不起作用。我研究了一下,发现的信息没有帮助,也没有三元运算符。这是密码

PHP


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

$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();

    }



}

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

<?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)){
         $nullResult = array();
        $nullResult['Email'] = (empty($email))? "empty" : "";
        $nullResult['Username'] = (empty($username))? "empty" : "";
        echo json_encode($nullResult);
        exit();
    }

        //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)){

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

        }else{

            echo "Test";
        }



}





?>


我仍然得到一个空的“”空在哪里?在$nullResult['Username']?是的,它假设类似于{“Email”:“empty”,“Username”:“false”}我得到了一个错误并修复了我,现在我得到了这个错误解析错误:语法错误,意外“;”在第38行的C:\wamp64\www\MT\pdovalization.php中,我不确定为什么要添加(!empty($data->rowCount()),因为我正在检查用户信息是否在数据库中,而不是错误。
<?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)){
         $nullResult = array();
        $nullResult['Email'] = (empty($email))? "empty" : "";
        $nullResult['Username'] = (empty($username))? "empty" : "";
        echo json_encode($nullResult);
        exit();
    }

        //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)){

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

        }else{

            echo "Test";
        }



}





?>