Tag: database

2009-12-10

Book List App: One Query to Rule Them All

by Charles — Categories: PHP, SQL — Tags: , , , 1 Comment

I got side-tracked from working on this project for awhile, but I am trying to get back to it now.

One thing I needed to address was that for many of the database tables I would have situations where a user supplies some data, and if it already exists in the table then I just need the primary key for it, or else I need to insert it and then get the key. Typically I’ve seen people do something along the lines of a select query to see if it’s in there, and if not perform a second query to do the insert.

While thinking tonight about how ugly that is, it came to me that maybe I could do an INSERT…ON DUPLICATE KEY UPDATE query. What I wasn’t sure about was if you could get the insert ID if it already existed and therefore does an update. Upon reading the MySQL manual entry on it, it looked like it should work, but I still wasn’t positive the PHP mysql_insert_id() function would work with it, and even less sure the related CodeIgniter database functions would. After a bit of testing, the good news is, it works. This should save me doing extra queries in a number of places, plus eliminate any need for table locking (I think).

So the basic process is:

public function replace($lastName, $firstName)
{
   $lastName = $this->db->escape($lastName);
   $firstName = $this->db->escape($firstName);
   $sql = "
INSERT INTO `author` (`last_name`, `first_name`)
VALUES ($lastName, $firstName)
ON DUPLICATE KEY UPDATE `last_name` = $lastName
";
   $this->db->query($sql);
   if($this->db->affected_rows())
   {
      return $this->db->insert_id();
   }
   return false;
}

Hopefully this will save me — and maybe you — a bit of processing time and coding time.

2009-09-16

Book List Application

by Charles — Categories: General, PHP — Tags: , , 1 Comment

As something I’m doing just because I want to and as a learning experience, I’m in the beginning stages of creating a web application where users can manage data on books: books they own, books they’ve read, and books they want to read. Assuming I stick with it, I figure I’ll post occasional articles here on the progress; hopefully providing some useful information on some of the issues I encounter as well as showing how my warped mind approaches such a task.

I started thinking about this a week or two ago, and posted a thread at KindleBoards.com to get some feedback on what sort of features people might like. I’m still in the process of deciding which suggestions to embrace and which to ignore. In the meantime I’ve started to forge ahead on the database design. I find that if I get the database structure right, then the rest falls into place much more easily — as opposed to making the database design fit my application code. My first major snag is trying to figure out how to deal with the fact that book titles are not unique (titles cannot be copyrighted). For details on that issue, I just started a thread at PHPBuilder.com, hoping some database expert will have a magical solution for me.

About the only firm decision at this point is that the app will be built upon the CodeIgniter framework, simply because I’m used to it, and it works. I’ve done some preliminary layout work for the front end, and have been making progress on using the Amazon.com Product Advertising API to (hopefully) provide a simple means for users to add books to their lists via a simple drag-and-drop of a URL from an Amazon web page. Unfortunately for us Amazon Kindle users, the Amazon database is currently not playing nice with Kindle books, so for now it’s limited to print book pages.

More to come soon, I hope….

© 2012 PHP Musings All rights reserved - Wallow theme v0.46.4 by ([][]) TwoBeers - Powered by WordPress - Have fun!