I am programming a page primarily in PHP and I was wondering if it would be better to grab a bunch of the non-sensitive info about a user from the database and put it into cookies for faster loading on future pages, or if I should have each page access the database several times?
The pages don't load particularly slowly, but there is a slight, noticeable delay for the sql query and PHP to populate the page. It could just be my server, but I would like to make sure my code is efficient before I worry about that.
Putting data in cookies means the user can alter it (which doesn't seem to be a problem for you), or that the user can erase it, perhaps by accident, requiring the data to somehow be filled again.
Why not put it in the session? It's managed on the server side, cached properly by any decent server, and you can reload data from the database if you create a new session.
You always need to keep in mind that each cookie that is set for a domain will be submitted by the browser with each request to that domain.
So you should avoid to store a huge amount of data in cookies because this has a negative impact on response time, especially for clients with a low upstream bandwidth.
Not sure what the query is, but you might be able to do a 50/50 solution by storing a UUID (some user-specific id number) in the cookie and then fetch the data from the table using that, ideally indexed, field.
This is essentially what the PHP session ($_SESSION) does, but just gives you another option as far as data storage or length of time the user should hold the data without having to mess around with all the session configuration options or creating a custom session handler.