Entries tagged "MySQL"

Avoid MySQL Queries Within Loops

One consistent piece of advice that's given on PHP help forums is that queries shouldn't be executed within loops. However, they don't usually provide an alternate means for accomplishing the task at hand. So I wanted to share a solution which fixed one of my scenarios. [Continue reading]

Why Does the fetch_array() Function Even Exist?

When looping through a MySQL result set, which function do you use? My preference has always been for the fetch_array() function since it allows access to the data using the database column names. However, is that the most efficient function to use? [Continue reading]

End PHP Scripts Gracefully After a Failed Database Connection

Last week's post talked about externalizing the MySQL connection script for easier maintenance. The problem with the previous code is that it's not very user friendly. If the database connection fails, it just drops everything and displays an error. Well, there's probably other content on the web page that can be viewed without database access. The website navigation, for example, most likely doesn't require a database. The navigation will probably lead to other pages that don't need that database. To minimize the impact to the visitor, let's look at a more graceful solution for handling connection failures. [Continue reading]

Maintain Your Database Login Information with Ease by Externalizing the Connection Script

When working with databases like MySQL, a connection needs to be established before processing any queries. That connection script could be added to the top of every page needing access, but what happens when the database password needs updating. Who wants to update dozens (or thousands) of files? As a solution, let's look at externalizing the code that makes the connection and importing it instead. [Continue reading]

Before Deleting a Database Table, Change Its Name

When a MySQL table is no longer needed, it could be deleted. However, are you sure that the necessary changes have been made so that the website is no longer connected to the table? It's tough to know for sure when the old database table is still available for querying. If the table is removed and it's still being used, it takes time to fix the connection(s). The database table could be restored until everything is fixed, but that's going to take time also. Instead, let's consider renaming the table before permanently deleting it. [Continue reading]

Minimizing MySQL Queries with the implode() Function

After seeing the many warnings that MySQL queries shouldn't be executed within loops, I finally broke down and figured out an alternate solution for the majority of queries where I use loops. Most times, a loop feels necessary when one database table contains the core information and another has multiple entries of supporting information. Instead of going for the typically loop, let's look at using the implode() function. [Continue reading]

Building the Where Clause for MySQL Dynamically

There are a number of ways to dynamically build the WHERE clause for a MySQL query. You could, for example, run several if() statements in the middle of a query. Or one could tap into the power of the implode() function. [Continue reading]

Formatting Dates within the MySQL Query

Before displaying dates from a MySQL database, do you change the format? If so, how are you formatting those dates? For me, I typically went straight to PHP for the answer. That is until it was brought to my attention that MySQL has a built in function for formatting dates. Let's talk about the date_format() function. [Continue reading]

Sorting Two MySQL Table Columns as One

How do you sort database entries chronologically when there are two different date fields? One shows when the entry was updated. The other indicates when it was created. If the developer didn't have the hindsight to set both fields to the same date when an entry is created, how do you work with the fields for sorting? [Continue reading]

Naming Your HTML Form Fields with an Associative Array

When using database entries to dynamically build HTML forms, how do you go about naming the form fields? Do you name them "Field1", "Field2′, etc.? Or do you have a more efficient way to access the fields when processing the form submissions? If you haven't tried using an array as the name, you may be missing out. [Continue reading]