Google Answers Logo
View Question
 
Q: PHP mySQL case then select inser question ( No Answer,   1 Comment )
Question  
Subject: PHP mySQL case then select inser question
Category: Computers > Programming
Asked by: piotrsienkiewicz-ga
List Price: $10.00
Posted: 15 Jun 2005 08:30 PDT
Expires: 18 May 2006 07:30 PDT
Question ID: 533532
Here is my current PHP/MySQL code:

$pub_query = $this->query("SELECT id FROM $db_table WHERE name = '$value'");		
  if ((mysql_num_rows($pub_query) == 0)) {
   //echo "ZERO";
   $this->query("INSERT INTO $db_table (name) VALUES ('$value')");
   return mysql_insert_id();
} else {
   //echo "NOT ZERO";
   $pub_results = mysql_fetch_array($pub_query);
   return $pub_results['id'];
}

This means that 2 queries are performed.. instead of 1. I'm currently
working with about 400,000 rows of data, and need to cut back where
ever I can.

I know there is a CASE statement in MySQL that may be able to do this.

CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list]
    [ELSE statement_list]
END CASE

If not maybe an IF statement that can get the same job done.
Answer  
There is no answer at this time.

Comments  
Subject: Re: PHP mySQL case then select inser question
From: soneji-ga on 16 Jun 2005 13:02 PDT
 
What you are trying to achieve is possible in mysql 4.0. You can find
some sample code on creating stored procs in MySQL on this link.

http://forums.mysql.com/read.php?98,19146,page=2

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy