News & Blog

Getting the First Word of a String with MySQL

November 10th, 2010 - Posted by Steve Marks to MySQL, Web Development.
I was recently assigned the task of getting the first word from a field in a MySQL table. In my case it was getting the first part of a customers postcode. Following a quick look through the MySQL reference manual I was guided towards the SUBSTRING_INDEX() function. By using this function ...

Inserting Multiple MySQL Records With One Query

November 3rd, 2010 - Posted by Steve Marks to MySQL, Web Development.
We can insert a single record into a MySQL table using the following format: INSERT INTO `table_name` (`column1`, `column2`, ...) VALUES ('value1', 'value2', ...) But how do we go about inserting more than one record at the same time? The answer is simple and involves us duplicating the la...

Back to Basics: Connecting to a MySQL Database using PHP

September 30th, 2010 - Posted by Steve Marks to MySQL, PHP, Web Development.
If you’re building a website using PHP then at some you’ll no doubt want to read and write data to a MySQL database. In order to do this we first need to establish a connection to the database. Presuming the database has already been setup we can do this by placing just a few l...

Perform a Mass MySQL Find and Replace in a Single Query

September 23rd, 2010 - Posted by Steve Marks to MySQL, Web Development.
If you haven’t heard of the MySQL REPLACE() function before then add it to your toolkit because I’m pretty confident you’ll need it again at some point. It’s saved my a** numerous times now and saved me hours of manual work. For those of you reading this that are fa...

MySQL Table Not Auto Incrementing Past 127

April 12th, 2010 - Posted by Steve Marks to MySQL, Web Development.
When inserting records into a MySQL table that contains an auto-incrementing field you might at some point start receiving the following error: “Duplicate entry ‘127’ for key 1” If this is the case it means the datatype of the field in question is set as ‘tiny...

Generating a Unique ID Using PHP and MySQL

April 7th, 2010 - Posted by Steve Marks to MySQL, PHP.
A recent e-commerce project I was working on required me to generate a unique order reference number containing both letters and numbers for every new order placed. Generating a unique reference was easy, but what if the generated output already existed in the database? Wouldn’t be v...