• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Categories
  • Contact Us
    • Advertising
    • Requests
    • Privacy Statement
HelpSpa.com

HelpSpa.com

Computer Tutorials

PHP/MySQL: How to Store Queries in a Separate Include File

October 14, 2009 by davidwank

When implementing a php/mysql database (or any database for that matter), it’s sometimes easy to have all of the queries located in one place, as opposed to having them hard-coded into the pages. If the queries are hard-coded in, and the queries are reused on multiple pages (which they often are), it can be a cumbersome task to update all of the queries. Having hard-coded queries also makes for larger, bulkier pages, which makes it harder to visualize the rest of the code on the page.

The following method is a simple solution to store all of your queries in one file and access them as needed. Please note that I am not advocating this as a “best practice” as there are many great articles on how to truly separate your presentation layer and your data layer, but this works.

I always have a database.php file that stores the connection data and has the related connection functions. So to continue with this “separation” concept, create a queries.php file.

To this file I simply add a variable that stores the specific query. So for example, I would add the following variables:

$getAuthors = "SELECT * FROM authors";
$getBooks = "SELECT * FROM book";

and so on with your queries.

Turning to the page that displays the results — the one that actually uses the query — (lets call it authors.php), I have the include statement:

include "queries.php";

and of course the code to access my database:

link_id = db_connect();
[here you can use whatever particular code you personally use to connect to a mysql database]

Then I simply populate the $sql variable (where I previously would have stored the actual code of the query), with the following:

$sql = $getAuthors;

To show you where it goes, I’m using the code: $result=mysql_query($sql) or DIE(mysql_error());

Again, the specifics don’t really matter other than that instead of hard-coding the sql query text for the $sql variable, you are simply referencing a variable in another file.

Now if you decide to update, change or modify the query, you can do it in one place — in the queries.php file.

So while this may not be the best method of handling this situation, it does work, and it does save time.

Filed Under: PHP, Programming Tagged With: Database, MySQL, PHP, Web Development

Primary Sidebar

More to See

Google Analytics 4 Migration Training

December 22, 2022 By davidwank

Hubspot Zoom Webinar Integration

Connecting HubSpot and Zoom Webinars

August 27, 2022 By davidwank

Google Analytics as we know it is going away!

July 24, 2022 By davidwank

Footer

Text Widget

This is an example of a text widget which can be used to describe a particular service. You can also use other widgets in this location.

Examples of widgets that can be placed here in the footer are a calendar, latest tweets, recent comments, recent posts, search form, tag cloud or more.

Sample Link.

Recent

  • Google Analytics 4 Migration Training
  • Connecting HubSpot and Zoom Webinars
  • Google Analytics as we know it is going away!
  • Building a New Dental Website?
  • BackupBuddy Timeouts on GoDaddy Hosting

Search

Tags

Adobe Acrobat Backup classical music and opera cPanel DAM Database Digital Photography email Excel Facebook Firefox hardware HP Security HTML iMovie Internet Explorer iTunes linksys Microsoft Office Microsoft Word MySQL networking Office 2010 OneNote OpenOffice OS X PDF PHP printers reseller hosting Reviews router SEO subversion Video Virtualization Virtual PC Web Design Web Development Web Hosting Windows Windows 7 Windows Vista Windows XP Wireless

Copyright © 2025 · Magazine Pro on Genesis Framework · WordPress · Log in