Archives For MySQL

I’m in the process of writing my latest book, “Effortless E-commerce with PHP and MySQL”, and as part of the process of writing any book, there’s lots of research involved. I want to check that I’m saying the right technical things (of course), but I also want to make sure that I’m doing things in an optimal way, that I’m using all the features and resources available to me, that I’ve reflected any recent changes in technology, etc. During this process, I just came across this article on MySQL’s SQL Profiler: Using the New MySQL Query Profiler. I was looking for the best way to time the execution of various queries (specifically to compare straight SQL with stored procedures) when I saw this, and I was glad I did.

I’ll leave it up to you to read the full article, but the gist of it is that if you’re using MySQL 5.0.37 or greater and using the command-line mysql interface, you can enable profiling to see exact performance numbers for the queries you run. You can even see the nitty-gritty details: everything MySQL does to run the query, how long each step takes, and even what CPU or memory usage was required.

Between the SQL Profiler and the EXPLAIN the command, you can quickly improve how efficiently your SQL commands execute, which will improve the performance of your Web application as a whole. And, in a surprising result, I found that the stored procedures I created ran much, much faster than straight SQL (this was on MySQL 5.1.37). Clearly MySQL has been taking great strides to improve the performance of stored procedures, which used to be notoriously inefficient.

For those of you interested in my forthcoming “Effortless E-commerce with PHP and MySQL” book, you can now look at the first example site at http://ecom1.dmcinsights.com. This project is covered in Part II of the book (four chapters total) and demonstrates:

  • Selling access to content (i.e., selling virtual products)
  • User management
  • Content management via administrative pages
  • Using PayPal’s Website Payments Standard system

There are instructions on the site for how you can access it and you can even download all the source code. There’s a form on every page through which you can ask questions or post comments. I welcome any and all feedback you may have!

I’m currently developing the second example site now, and will also make that publicly available when it’s ready. Thanks!

Normally two Models in an MVC architecture are related to each other, such as Employees and Departments (to use the classic example), where each employee is in one department and each department has multiple employees. Although Yii does a great job of auto-generating most of the code you need, including the form used to create and update a Model, the Yii-generated form won’t properly represent the related Model. In this post I’ll walk you through what you need to do to make your forms work properly for related Models. Continue Reading…

For the past couple of weeks I’ve been working full-bore on my next book, titled “Effortless E-commerce with PHP and MySQL” (the name has changed slightly since its original), which is why I haven’t been able to post much on the blog. I’m currently working on Chapter 5 , which is the penultimate chapter for the first of the two e-commerce examples being developed. Here’s how the first six chapters are shaping up:

Part 1: Fundamentals

Chapter 1: Getting Started

  • Identifying Your Business Goals
  • Researching Legal Issues
  • Choosing Web Technologies
  • Selecting a Web Host
  • Using a Payment System
  • The Development Process

Chapter 2: Security Fundamentals

  • Security Theory
  • PCI Requirements
  • Server Security
  • Using Secure Transactions
  • Common Vulnerabilities

———————

Part 2: Selling Virtual Products

Chapter 3: First Site: Structure and Design

  • Database Design
  • Server Organization
  • Connecting to the Database
  • The Config File
  • The HTML Template

Chapter 4: User Accounts

  • Defining Helper Functions
  • Registration
  • Logging In
  • Logging Out
  • Managing Passwords
  • Improving the Security

Chapter 5: Managing Site Content

  • Creating an Administrator
  • Adding Pages
  • Displaying Page Content
  • Adding PDFs
  • Displaying PDF Content
  • Recommended Alterations

Chapter 6: Using PayPal

———————

Chapter 1 is an overview of the process along with tips for making certain decisions, such as hosting. Chapter 2 is generally a big-picture look at security, along with what primary decisions (e.g., hosting, certificate types) you’ll need to make. At the end of the chapter, I discuss the most common server vulnerabilities and attacks and how you go about preventing those. Although the chapter is really about an approach to security, it has some exact recommendations as well. More specific security techniques are demonstrated throughout the rest of the book.

In Chapters 3-6, you’re creating a entire site for selling access to online content, in both HTML and PDF formats. You’ll learn some ways to automate processes and effectively separate out bits of code to make the site easy to maintain. I don’t do anything fancy with the HTML templates, the PHP, or the MySQL, but there’s some great application of those technologies (in other words, you’ll learn a lot but won’t be overwhelmed with stuff like Smarty, OOP, etc.).

In Chapter 4 I created some really great helper functions. I even impressed myself with their usefulness and simplicity. The chapter also handles passwords and form validation in very secure ways. Chapter 4 concludes with some other ways you can heighten the security of the system, when you deem that appropriate. Chapter 4, User Accounts, may be something you’re familiar with overall (perhaps from another book of mine), but I think I’ve added enough new ideas here to still give you some value for reading it.

In Chapter 5 you learn how to add content to the site. There are two types. For the first, you can add HTML content using a WYSIWYG editor, that I show you how to integrate. For the second, you can upload PDFs. The PDFs will only be served through a proxy script so that a user must be logged in (with an account that hasn’t expired) to access them. The chapter wraps with discussions of about six different features you could add to the site, almost all intended to make it a better site for the potential customers. Although I don’t walk through the every add-on in complete detail, you’ll see what other tables you’d need to create, what SQL commands might be involved, and how the PHP code would work. I like this addition to the chapter and hope you will, too.

Chapter 6 integrates PayPal. You’ll learn a bit about how PayPal works and what its strengths and weaknesses are. Then you’ll use PayPal’s sandbox to test your site and see what you need to do to take the site live.

Next week I’ll be starting on Part 3 of the book, in which you’ll create an online site that sells coffee (beans, not brewed). I choose coffee as a product for a couple of reasons. First, I love coffee. Second, it’s an example of a physical product that must be shipped, that has inventory, and that also has permutations–size, roast, whole beans/ground–without being too, too complicated.

So that’s where things stand. Thanks for your interest in the book and please share any questions and comments you may have!

On a recent Yii-based project, managing one of the Models required a whole slew of checkboxes to indicate that yes, the quality does apply, or no, it does not. In this case, the value being stored in the database for each attribute was a single letter: Y/N. However Yii, when showing the form to update an item, needs the checkbox value to be a Boolean, in order to properly pre-check the box. Changing the database wasn’t an option in this case, so I had to figure out a good conversion process. In this post, I’ll tell you exactly how I solved this issue. Continue Reading…