I did something similar, I think you need to add the type of variable that is in the database (INT, VARCHAR, LONGTEXT ...). I solved this problem by creating a table with all the types that could be created. That is, first create a table 'fields' with id, name and type that looks like this
___________campos_________
ID NOMBRE TIPO
1 xg VARCHAR(255)
Once this is done, in the function in which you create the new column, before creating it, you make a query like the following:
$sql = "SELECT tipo FROM campos WHERE nombre = 'xg';";
Now we would stay (assuming we are already connected to the bbdd with mysqli)
$resource = $conn -> query($sql);
$campoDetalles = $resource -> fetch_object();
$infoExtra = $campoDetalles -> tipo;
Once this is done, you only have to do what you were doing with the new info
$sql = "ALTER TABLE <la_tabla> ADD ".$variable." ".$infoExtra.";";
I hope it helps you!