如何附加到json_encode数组

时间:2017-05-18 06:09:43

标签: php sql arrays json string

我有一个验证检查器,检查数据库以查看"电子邮件和用户名"正在使用,如果是,那么回复电子邮件和用户名为" false"。如果它不在数据库中,则回显到" true"。我将返回用户电子邮件和用户名在sql服务器中的结果。所以我想追加" $ json1或$ json2"到第一个或第二个" $像这样

 {"Email":false, "Username":true};

PHP代码:

<?php

if(isset($_GET['submit'])){

    //Connect to database
    $link = mysqli_connect("localhost", "root", "");
    mysqli_select_db($link, "magicsever");

    if(mysqli_connect_error()){

        die ("Database connection error");
    }

    //Find the email in the database
    $query = "SELECT * FROM app_signup WHERE email = '".mysqli_real_escape_string($link, $_GET['email'])."'";
    $query2 = "SELECT * FROM app_signup WHERE username = '".mysqli_real_escape_string($link, $_GET['username'])."'";

    $result = mysqli_query($link, $query);
    $result2 = mysqli_query($link, $query2);

    $email_checker1 = json_encode(array("Email"=>false));
    $email_checker2 = json_encode(array("Email"=>true));

    $username_checker1 = json_encode(array("Username"=>false));
    $username_checker2 = json_encode(array("Username"=>true));

    if(mysqli_num_rows($result)>0){

        echo $email_checker1;


    }

}
?>

5 个答案:

答案 0 :(得分:1)

为电子邮件和用户名结果制作新数组

//Find the email in the database
$query = "SELECT * FROM app_signup WHERE email = '".mysqli_real_escape_string($link, $_GET['email'])."'";
$query2 = "SELECT * FROM app_signup WHERE username = '".mysqli_real_escape_string($link, $_GET['username'])."'";

$result = mysqli_query($link, $query);
$result2 = mysqli_query($link, $query2);

$dataResult = array();
$dataResult['Email'] = (mysqli_num_rows($result)>0)? false: true;
$dataResult['Username'] = (mysqli_num_rows($result2)>0)? false: true;

echo json_encode($dataResult );

答案 1 :(得分:0)

JSON只是一种数据格式,是一种呈现数据结构的方式。在您拥有可以呈现的数据结构之前,您不希望将其转换为JSON。

您应该做的是构建一个包含验证错误的数组:

// I'm assuming that the true/false means that there was an error with validating that value
// You can initialize the validation array with false values for all fields and change them to true in case of validation error
$errors = ['Email' => false, 'Username' => false];

// Then, change the values in case of an error
if (... /* invalid email */) {
    $errors[] = ['Email' => true];
}
if (... /* invalid username */) {
    $errors[] = ['Username' => true];
}

然后仅在显示数组时将数组转换为JSON:

echo json_encode($errors);

答案 2 :(得分:0)

为什么不在数组附加后执行json_encode。

$first = json_encode(array("Email"=>false, "Username"=>false));

加入。

$data['Email'] = false;
if('''')
{
  $data['Username'] = false;
}
$result = json_encode($data);

答案 3 :(得分:0)

 $first = json_encode(array("Email"=>false));
    $second = json_encode(array("Email"=>true));

    $json1 = json_encode(array("Username"=>false));
    $json2 = json_encode(array("Username"=>true));

像这样使用

$first = json_encode(array("Email"=>false)+array("Username"=>true));
$second = json_encode(array("Email"=>true)+array("Username"=>false));

答案 4 :(得分:0)

如果您真的希望将它们合并在一起,请阅读此参考资料

好的方法就像

$resData1['email']=false;
$resData2['email']=true;

$resData1['Username']=false;
$resData2['Username']=true;

$json1 = json_encode($resData1);
$json2 = json_encode($resData2);