Failed to convert nvarchar'Array' value to int data type

Asked

Viewed 41 times

-1

Hello. I’m having some trouble inserting values into a table.

Explaining better, I get the AID generated in Account and need to insert it in Login. I was successful in consulting the AID generated in Account but I have problems inserting it, the error is as follows:

Notice: Array to string Conversion & Fatal error: Uncaught Pdoexception: SQLSTATE[22018]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Failed to convert the nvarchar 'Array' value to the int data type.

The word array is to be the generated AID number, but in the error it sends with the word Array, but in my var_dump ($result) it displays the number, it confuses me a little.

Follows the code:

    public function save(){
    $query = "insert into Account(UserID, UGradeID, PGradeID, RegDate, Name, Email)values(:userid, 0, 0, GETDATE(), :name, :email)";
    $stmt = $this->db->prepare($query);
    $stmt->bindValue(':userid', $this->__get('userid'));
    $stmt->bindValue(':name', $this->__get('name'));
    $stmt->bindValue(':email', $this->__get('email'));
    //$stmt->bindValue(':password', $this->__get('password'));
    $stmt->execute();

    $sql = "SELECT AID FROM ACCOUNT WHERE UserID = :userid";
    $statement = $this->db->prepare($sql);
    $statement->bindValue(':userid', $this->__get('userid'));
    $statement->execute();
    $result = $statement->fetch(\PDO::FETCH_ASSOC);
    $result['AID'];
    //$result->result;

    print_r($result);

    $insert = "INSERT INTO LOGIN(UserID, AID, Password) VALUES (:userid, :result, :password)";
    $stmt2 = $this->db->prepare($insert);
    $stmt2->bindValue(':userid', $this->__get('userid'));
    $stmt2->bindParam(':result', $result);
    $stmt2->bindValue(':password', $this->__get('password'));
    $stmt2->execute();

    return $this;
}

1 answer

0

I don’t quite understand your question, but try to add the type
Following example.

Individually adding.

$stmt->bindValue(':userid', $this->__get('userid'),PDO::PARAM_INT);

Putting it all together dynamically.

foreach ($dados as $key => $value) {
    if (is_int($value))
        $param = PDO::PARAM_INT;
    elseif (is_bool($value))
        $param = PDO::PARAM_BOOL;
    elseif (is_null($value))
        $param = PDO::PARAM_NULL;
    elseif (is_string($value))
        $param = PDO::PARAM_STR;
    else
        $param = false;

    if ($param) $stmt->bindValue(":$key", $value, $param);
}   
  • I appreciate your willingness to help me, but I figured out the problem. It was the way in which I accessed the result query that was wrong, it was necessary to assign another variable and do the search by AID. In this case of the main post, it was recovering all query, not only AID.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.