mysqli_query没有按预期工作

I have the php:

$con=mysqli_connect("127.0.0.1","foo","bar","quaz");
if($con){
    $sql = "INSERT INTO `virtual_users` (`domain_id`, `password` , `email`) 
    VALUES ('2', ENCRYPT('".$password."', CONCAT('\$6\$', SUBSTRING(SHA(RAND()), -16))), '".$user."@".$domain."');";

    $query = mysqli_query($con, $sql);

    if(!$query){
        echo $sql;
    }else{
        echo "Success adding new email user!";
    }
}

For some reason when I run this query it always returns $sql. This means that the connection is fine but the query is not.

When I then run the the echo of $sql directly on the mysql database it works perfectly!! I have no idea what is going wrong! Any ideas?

That's because you should be outputting the reason for the failure, not the query that caused the failure:

$result = mysqli_query($con, $sql) or die(mysqli_error($con));
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^

There's exactly ONE way for a query to succeed, and a near infinite number of ways for it to fail. Just having "valid" sql means nothing.