Неопределенная переменная в PHP json_encode - PullRequest
0 голосов
/ 10 апреля 2019

Я получаю неопределенную переменную PHP в моем коде. Я использую PDO для json_encode вывода. Когда я тестирую свой код, я получаю эту ошибку. Как я могу исправить и избежать этого в будущем? Моя структура кода в порядке? или мне нужно улучшить его, чтобы избежать этой проблемы

Неопределенная переменная: ar

$sql = $conn->prepare("SELECT UserID, UsrPassword, ContactID, UserTypeID, UserStatus, ClientUpdate, ServerUpdate, Deleted FROM tblUsers WHERE ContactID = :contactid AND ServerUpdate > :lastchecked AND Deleted != :deleted", $cursor);
$sql->bindValue(":contactid", $ContactID);
$sql->bindValue(":lastchecked", $LastChecked);
$sql->bindValue(":deleted", 1);

if($sql->execute()){
    $count = $sql->rowCount();

    if($count > 0){
        while ($row = $sql->fetch()) {
            $decr = CryptRC4(FromHexDump($row['UsrPassword']), $key);

            if($row['ServerUpdate'] > $row['ClientUpdate']){
                $lupdate = date("Y-m-d h:i:s", strtotime($row['ServerUpdate']));
            }
            else{
                $lupdate = date("Y-m-d h:i:s", strtotime($row['ClientUpdate']));
            }

            $ar[] = array(
                'UserID' => $row['UserID'],
                'UsrPassword' => $decr,
                'ContactID' => $row['ContactID'],
                'UserTypeID' => $row['UserTypeID'],
                'UserStatus' => $row['UserStatus'],
                'Deleted' => $row['Deleted'],
                'LastUpdated' => $lupdate
            );
        }
    }
}
else{
    $ar[] = array(
        "Message" => $sql->errorInfo(),
        "sql" => $sql
    );
}

print json_encode($ar);

Ответы [ 2 ]

1 голос
/ 10 апреля 2019

вам необходимо объявить переменную $ ar в виде массива перед оператором if, поэтому область действия переменной не ограничена, и вы можете получить к ней доступ за пределами If, если еще.

    $sql = $conn->prepare("SELECT UserID, UsrPassword, ContactID, UserTypeID, UserStatus, ClientUpdate, ServerUpdate, Deleted FROM tblUsers WHERE ContactID = :contactid AND ServerUpdate > :lastchecked AND Deleted != :deleted", $cursor);
    $sql->bindValue(":contactid", $ContactID);
    $sql->bindValue(":lastchecked", $LastChecked);
    $sql->bindValue(":deleted", 1);
    $ar = array();

    if(){
     // You code goes here
    }
    else{
     // You code goes here
    }

    print_r($ar);
0 голосов
/ 10 апреля 2019

кажется if($count > 0){ условие не выполнено, объявите ранее, надеюсь, это поможет

$sql = $conn->prepare("SELECT UserID, UsrPassword, ContactID, UserTypeID, UserStatus, ClientUpdate, ServerUpdate, Deleted FROM tblUsers WHERE ContactID = :contactid AND ServerUpdate > :lastchecked AND Deleted != :deleted", $cursor);
    $sql->bindValue(":contactid", $ContactID);
    $sql->bindValue(":lastchecked", $LastChecked);
    $sql->bindValue(":deleted", 1);
    $ar = array();

    if($sql->execute()){
        $count = $sql->rowCount();

        if($count > 0){
            while ($row = $sql->fetch()) {
                $decr = CryptRC4(FromHexDump($row['UsrPassword']), $key);

                if($row['ServerUpdate'] > $row['ClientUpdate']){
                    $lupdate = date("Y-m-d h:i:s", strtotime($row['ServerUpdate']));
                }
                else{
                    $lupdate = date("Y-m-d h:i:s", strtotime($row['ClientUpdate']));
                }

                $ar[] = array(
                    'UserID' => $row['UserID'],
                    'UsrPassword' => $decr,
                    'ContactID' => $row['ContactID'],
                    'UserTypeID' => $row['UserTypeID'],
                    'UserStatus' => $row['UserStatus'],
                    'Deleted' => $row['Deleted'],
                    'LastUpdated' => $lupdate
                );
            }
        }
    }
    else{
        $ar[] = array(
            "Message" => $sql->errorInfo(),
            "sql" => $sql
        );
    }

    print_r(json_encode($ar));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...