MySQL/PHP Tips & Tricks
Last Updated: October 27, 2009
This page is still very much under development, I'll add to it as I have time. These tips are MySQL-related and most also involve PHP. Each tip is followed by a series of search terms which I hope will help other developers locate these tips that I found useful.
MySQL Country Table
A table creation script and zipped file of miniature flags for over 240 countries. Includes the name of the country in two forms (short and long), 2 and 3 character ISO codes, and a numeric ISO code. Also included is a yes/no field specifying whether the nation is a member of the United Nations.
Displaying MySQL Dates in Other Formats
A brief summary of how to use PHP to display dates pulled from a MySQL database in various alternative formats. Formats include "October 01, 2009", "Thursday, October 01, 2009", and "Thursday, the 1st of October, 2009", among others. View Page
List of States as MySQL Table & PHP Function for Select Box
A MySQL script for generating a table consisting of a list of the 50 US states, Washington DC, and the three Armed Forces abbreviations: AA, AE, and AP. Also included is a PHP function for accessing the database and a PHP function for displaying the data in a select box. View Page
How to Get the Last Auto-Incremented Number From MySQL
When you need to insert data into more than one table at a time in a normalized database with relationships between the tables, you will often need to obtain the record number of the data inserted into the first table before creating your query to insert data into the second table. This can be accomplished with a short series of commands. Use the following code immediately after you make the first query:
/* This query figures out what the last auto-
incremented number was so that it can be
used in the next query or other action.
This assumes you are using the $link variable
used in most MySQL examples to store the
information necessary to connect to your
database. */
$query = "SELECT LAST_INSERT_ID()";
$result = mysql_query($query, $link);
$row = mysql_fetch_array($result);
// following is the newly created record
$last_id = 0 + $row[0];
