[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.6

MySQL: Inserting Unique Column Value

Asked by headzoo in MySQL Server

Tags: MySQL

Hi,
Scenario: I have a database table that stores blog posts. It looks like this:

CREATE TABLE `posts` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 50 ) NOT NULL ,
`safe_title` VARCHAR( 60 ) NOT NULL ,
`content` TEXT NOT NULL ,
`user_id` INT UNSIGNED NOT NULL ,
UNIQUE (`safe_title`)
) ENGINE = MYISAM ;

The `safe_title` column contains the post title in a URL safe format. That is, the post title with all special characters removed. So a post title of "Hello World" becomes "hello_world". It's important that every post has a unique safe title, even if some posts have the same title. Post #1 with the title "Hello World" would have the safe title of "hello_world". Post #2 with the title "Hello World" would have the safe title "hello_world_2", and so on.

The problem: How to insert a unique safe title in only one or two db transactions? I don't want to create code like this:

$found = true;
$counter = 0;
do {
      ++$counter;
      $safe_title = getSafeTitle('Hello World') . '_' . $counter;
      $results = $db->execute("SELECT FROM `posts` WHERE `safe_title` = '$safe_title' LIMIT 1");
      if (count($results) == 0) {
            $found = false;
      }
} while ($found == true);
$db->insert("INSERT INTO ....") // Insert post with safe title

That little bit of code could loop through a dozen times before a unique title is found. Even worse, by the time $db->insert() is called, the safe title that I've found may no longer be unique (If the system is inserting thousands of new blog posts a minute).

So how do I avoid this problem?
[+][-]06/16/08 12:41 AM, ID: 21791428Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: MySQL Server
Tags: MySQL
Sign Up Now!
Solution Provided By: pzurowski
Participating Experts: 3
Solution Grade: A
 
[+][-]06/15/08 11:25 PM, ID: 21791106Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06/16/08 12:01 AM, ID: 21791270Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/16/08 12:16 AM, ID: 21791322Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06/16/08 01:01 AM, ID: 21791524Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/16/08 01:28 AM, ID: 21791659Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06/16/08 01:31 AM, ID: 21791678Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/16/08 01:36 AM, ID: 21791696Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06/16/08 03:28 AM, ID: 21792213Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06/16/08 03:30 AM, ID: 21792225Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06/16/08 03:43 AM, ID: 21792292Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 / EE_QW_2_20070628