query

Handling Drupal SQL exceptions (db_insert, db_update, db_delete)

While developing a Drupal module, I just ran into a situation where it may be common for users to generate SQL exceptions. I have a 'unique' limit on several of my database tables, essentially saying that certain name fields must be unique for the current project. As you can imagine, it's extremely easy to enter a duplicate name, and while doing a SQL INSERT that can easily lead to a SQL exception.

As a result, I dug around and found a way to handle SQL exceptions in Drupal queries. My current problem is with a Drupal 7 db_insert query, and I handled it like this:

A Drupal 7 table with sortable headers and pager

In my previous blog post I wrote about how to create an HTML table in a Drupal 7 module form. As promised in that tutorial, once I figured out how to make the table columns sortable, I'd share that secret recipe as well. Here then is how to make an HTML table in a Drupal 7 module/form:

Case insensitive SQL SELECT query examples

Case insensitive SQL SELECT query FAQ: How do I issue SQL SELECT queries while ignoring case (ignoring whether a string is uppercase or lowercase)?

When I first started writing SQL queries, I did some pretty crazy things to try to ignore case in my text/string queries. Unfortunately for me, I first started with Postgresql, and used some of their custom regular expression capabilities. That seemed like a good idea at the time, but when I tried to move my application to a MySQL database, I paid a price in having to rewrite my case-insensitive SQL queries.

How to run a Drupal 7 query outside of Drupal

Drupal query FAQ: How can I run a Drupal query outside of Drupal, i.e., from the command line? (Drupal 6 or Drupal 7)

Last night I was reading the excellent book Pro Drupal 7, and I stumbled on most of the solution for running a Drupal query from the command line. The book was exactly correct, except for one missing step, which may have been something that changed after publication.

CakePHP find conditions (plus find all, find list, and find count)

CakePHP find FAQ: Can you share some "CakePHP find conditions" examples?

One thing you need to get used to when working with CakePHP is the CakePHP find method. To that end, I thought I'd put together this page of CakePHP find conditions and find query examples.

CakePHP find conditions

To get started, here's a list of the possible CakePHP find conditions you can use:

A PHP "select from where in" query example

If you ever need the source code for a PHP "SELECT FROM foo WHERE id in (1,2,3)" example, I hope the following PHP function will be a decent example to work from. I turned a given list of database table id values into a CSV list, then use that string in a "SELECT FROM ... WHERE ... IN ... (list)" SQL query.

I'm not going to explain it today, but I will share the PHP source code for your reading enjoyment (and problem solving).

PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

PHP FAQ: What is the PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in error message, and how do I fix it?

I just ran into this this "mysql_fetch_array() expects parameter 1 to be resource, boolean given" error message, and learned that it's because there's something wrong with my SQL query.

A MySQL PHP update query example

Nothing major here, just an example MySQL PHP UPDATE query (really, a PHP SQL UPDATE query example that happens to use a MySQL database). For all the things I've done lately with PHP, I think this is the first PHP update query I've written, and I wanted to put an example out here so I can find it easily later.

So, with no further ado, here is a simple MySQL PHP update query example:

A PHP/MySQL script to query the Drupal comments table

PHP/MySQL FAQ: Can you share an example of a PHP script that is used to query a MySQL database?

This weekend I finally fixed a minor/known bug in my PHP/MySQL script that I use to generate the Drupal sitemap for this website. (A PHP script that queries a Drupal MySQL database.) I like to update the sitemap whenever a blog post has changed, and I had all of that working, except I wasn't accounting for changes due to user comments that are approved.

CakePHP find - a SQL limit query example (SQL select with limit clause)

When working with CakePHP, one of the hardest things for me has been getting used to formulating SQL queries with the CakePHP find method syntax. Today I needed to run a SQL query where I limited the amount of records returned from the database, and after a little trial and error, I found the correct CakePHP SQL LIMIT syntax, and thought I'd share that here today.

Syndicate content