This website recently got a major rebuild; if you're missing something, let Lorna know.

    Tag: mysql


  1. Use Database Connection Strings with Laravel 8


    I've been doing a lot of database stuff lately, and not much PHP, so when I returned to make my first Laravel project for a while, I had to check the docs to remind myself how some of this works. I noticed that the default approach to database credentials is still to use separate credentials for the host, port, and other variables. I'm using Aiven databases (because I work there and managed databases are great for demo apps as well as real ones!) which supply connection strings, but Laravel supports these too.

    Read more

  2. Connecting PHP to MySQL on Bluemix


    Most of the PHP I write runs on Bluemix - it's IBM self-service cloud, and since I work there, they pay for my accounts :) There are a bunch of databases you can use there, mostly open source offerings, and of course with PHP I like to use MySQL. Someone asked me for my connection code since it's a bit tricky to grab the credentials that you need, so here it is.

    Read more

  3. MySQL 5.7 Introduces a JSON Data Type


    There's a new JSON data type available in MySQL 5.7 that I've been playing with. I wanted to share some examples of when it's useful to have JSON data in your MySQL database and how to work with the new data types (not least so I can refer back to them later!)

    MySQL isn't the first database to offer JSON storage; the document databases (such as MongoDB, CouchDB) work on a JSON or JSON-ish basis by design, and other platforms including PostgreSQL, Oracle and SQL Server also have varying degress of JSON support. With such wide adoption as MySQL has, the JSON features are now reaching a new tribe of developers.

    Read more

  4. From MySQL to MailChimp via CSV


    Don't you hate disclaimers? I do, but before I do anything else, I must ask that you don't use the techniques below unless you are emailing responsibly.

    Today I needed to pull email addresses for people who had signed up to a thing out of MySQL and into MailChimp so that I could actually email them about the thing. MySQL actually has a very cute feature for exporting the results of an SQL query as a CSV file, which I had to look up to remember how to do it. It goes something like this:

    Read more

  5. SQL JOINing a Table to Itself


    Getting two sets of information from one table in a select statement often leads people to write subselects, but it really doesn't matter that this is the same table twice, we can just give it a new alias and treat it as if it were a different table. This is one of those techniques where, once you've seen it, it's really obvious, but until that point it can be very confusing. I explained this to someone else recently, so I thought I'd capture it here in case it's helpful to anyone else.

    Consider that tried-and-tested example: employees and managers. Here's the staff table from the database (today's imaginary data isn't particularly imaginative, sorry):

    Read more

  6. SQL Joins with On or Using


    I recently wrote a post about inner and outer joins, and a couple of people asked what the difference is between USING and ON.

    Read more

  7. Inner vs Outer Joins on a Many-To-Many Relationship


    Someone will probably tell me that this is an elementary-level topic, but I got some good questions regarding joins from my most recent ZCE class students, so I thought I'd put down the examples that I used to explain this to them. Being able to join with confidence is a key skill, because it means that you can refactor and normalise your data, without worrying about how hard something will be to retrieve.

    Read more

  8. Explaining MySQL's EXPLAIN


    The MySQL explain plan is a great tool to help developers and database administrators to improve the performance of specific queries happening against a database. It is very easy to use, but its output can be confusing, so I thought I'd show a very simple example.

    Read more

  9. Handling SQL Errors in PDO


    I love PHP's PDO (PHP Data Objects) extension; it gives a consistent, object-oriented interface to handling all kinds of relational database backends. One thing that annoys me is that the MySQL driver for PDO defaults to a silent error mode which can make SQL errors tricky to spot!

    Read more

  10. PHPMyAdmin Designer View


    This week I've been using phpMyAdmin for what feels like the first time in years. I'm happier at the command line, but needed some graphical representation of information and easy ways to export example queries for the book I'm working on. I noticed that phpMyAdmin now has a Designer tab, which shows relationships between tables and allows you to define them.

    |image1|

    Read more

  11. Gearman Priorities And Persistent Storage


    I have been writing a bit about Gearman lately, including installing it for PHP and Ubuntu, actually using it from PHP and also how I use persistent storage with Gearman. I'm moving on to look at adding jobs of different priorities.

    I use Gearman entirely as a point to introduce …

    Read more

  12. Using Persistent Storage with Gearman


    I'm using gearman for the first time in a new project, and two things in particular were bothering me. Firstly, there doesn't seem to be a built-in way to see what's in the queue. Secondly, if the gearman server dies (which seemed quite likely when I was first getting to grips with this stuff and writing really buggy code!) you lose your queue. Therefore I decided that I would switch gearman over to running with persistent storage.

    Read more

  13. Dealing with MySQL Gone Away in Zend Framework


    I wrote recently about having gearman in my application, however I have been seeing problems with the long-running PHP worker scripts. My logs had entries like this:

    SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
    

    The worker is a Zend Framework application, run from the CLI, and it …

    Read more

  14. Is Enum Evil?


    When I work on database designs, either on my own projects or as advisor to others, I often find people very reluctant to use an enum type for any columns. Now, I'm not about to advocate the gratuitous use of an enum column, they definitely have some pitfalls, but I think it is important to understand these rather than just shouting "enum evil!" if anyone should mention them.

    There are cases where an enum is the correct choice for a particular type of data, so let's look at what an enum type actually is and does.

    Read more

  15. Mysql Error


    I was running an import script today taken from a mysqldump from another user, when I saw an error that looked like this:

    ERROR 1005 (HY000) at line 123: Cant create table  (errno: 150)
    

    This is caused by a mysqldump or export process exporting tables in alphabetical order and not …

    Read more