I am using PDO to talk to my database, and I wonder if casting a type like this
$dbh->query("SELECT * FROM recipes WHERE id=".(int)$id);
is sufficient to prevent sql injection? In this case $id is always an integer.
I also wonder what would be a good way to prevent an injection in this kind of statement if the variable was a string.
Yes. Casting to int prevents all the nasty SQL injection possibilities.
If the variable were a string, you should use prepared statements to pass it.