Does a dynamic stylesheet need to have a separate database connection?

Hey guys,

I’ve got a stylesheet that is dynamically generated, pulling its data from a database. Currently, in order to retrieve the stylesheet data I have to open a new database connection inside the file.

I was wondering if there actually exists any work around to ensure I don’t have more than a single db connection i.e. use the database connection that the rest of the pages are using instead.

Many thanks.

yes, it will be a separate DB connection, as the file is a new request to the server. Why not just cache the rendered CSS server side so it doesn’t need to be rebuilt so often.

Just to clarify, are you talking about an external stylesheet?

In other words, you want to use the same database connection for when someone pulls data from your PHP page as the one you use when someone pulls an external CSS file (in a separate HTTP request)?

I know I’m a few days late in this thread, but thought I would check anyway…

Yep, same rules apply.

If conn.php creates an instance of a Database class, would the scope of that object be the stylesheet AND all other pages that include it?

Use an include() and put your connection in the included file.


include('conn.php');

/* rest of sheet */

Using an include allows you to use it in any file that needs a databse call.