Entries tagged "MySQL"

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]

Going Beyond the Typical Sort: Sorting by Specific Values with MySQL’s Order By Clause

When performing MySQL database queries, have there been times where the "Order By" clause doesn't seem to cut it? Well it turns out that we can do more than sort by one or more columns in ascending and descending order. MySQL's FIELD() function provides a way to target a specific value from within a column. Let's take a closer look at the function. [Continue reading]

Sorting HTML Data Tables Part 2: Dynamically Sort in Ascending and Descending Order

In last week's post we looked at dynamically sorting HTML data tables. But we only talked about sorting the columns in either ascending or descending. If the user is looking for a last name that appears near the end of the alphabet and the column is sorted from A to Z, they may have a lot of names to go through before finding the one they want. Instead we could provide an option for sorting in both descending and ascending order. [Continue reading]

Sorting HTML Data Tables Using the Header Row with PHP

When displaying data tables, how do you usually sort the information? Do you sort by the field that you think is the most important to the visitor? For example, it's perfectly acceptable to list entries chronologically; where the newest is shown first. But what if the user wants the data organized differently? We could let them choose. [Continue reading]