Re: Compare and inserting with php

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jun 30, 2009 at 11:17 AM, Ashley
Sheridan<ash@xxxxxxxxxxxxxxxxxxxx> wrote:
> On Tuesday 30 June 2009 15:42:01 Miller, Terion wrote:
>> Okay, what I am trying to do is this:
>>
>> I have a db full of inspections, daily I need to run a script that will
>> grab the posting of inspections, check them against the db to make sure
>> they aren't duplicates and then insert all the new entries..... Not sure if
>> my logic of going about it this way is right, or if I'm off track and need
>> to be doing something different please advise...so far this is how I am
>> writing it.....and it seems redundant ... Shouldn't there be a way to say
>> (in plain speak) if there are no records with the name== insert this one,
>> if there is one with the name then check intype if none ..insert... Etc
>>
>>
>>  if (!empty($Go)) {
>>
>> $query = "SELECT * FROM restaurants WHERE name LIKE '$ucName'";
>>
>>  $result = mysql_query ($query);
>>
>>  $row = mysql_fetch_object ($result);
>>
>>
>>  If (mysql_num_rows($result) == 0) {
>>
>> $sql = "INSERT INTO `restaurants` (name, address, inDate, inType, notes,
>> critical, cviolations, noncritical)  VALUES (";
>>
>> $sql .= " '$ucName', '$ucAddress', '$inDate', '$inType', '$notes',
>> '$critical', '$cleanViolations', '$noncritical')";
>>
>>         $result = mysql_query($sql) or die(mysql_error());
>>
>>
>>     } else {
>>
>>  $query = "SELECT * FROM restaurants WHERE inType LIKE '$inType' AND name
>> LIKE '$ucName' ";
>>
>>   $result = mysql_query ($query);
>>
>>             $row = mysql_fetch_object ($result);
>>
>>     If (mysql_num_rows($result) == 0) {
>>
>>    $sql = "INSERT INTO `restaurants` (name, address, inDate, inType, notes,
>> critical, cviolations, noncritical)  VALUES (";
>>
>>              $sql .= " '$ucName', '$ucAddress', '$inDate', '$inType',
>> '$notes', '$critical', '$cleanViolations', '$noncritical')";
>>
>>         $result = mysql_query($sql) or die(mysql_error());
>>
>>
>>         } else {
>>
>>   $query = "SELECT * FROM restaurants WHERE inDate LIKE '$inDate' AND name
>> LIKE '$ucName' ";
>>
>>                $result = mysql_query ($query);
>>
>>                $row = mysql_fetch_object ($result);
>>
>>
>>   If (mysql_num_rows($result) == 0) {
>>
>>   $sql = "INSERT INTO `restaurants` (name, address, inDate, inType, notes,
>> critical, cviolations, noncritical)  VALUES (";
>>
>>                $sql .= " '$ucName', '$ucAddress', '$inDate', '$inType',
>> '$notes', '$critical', '$cleanViolations', '$noncritical')";
>>
>>
>>        $result = mysql_query($sql) or die(mysql_error());
>>
>>
>>    }
>> }
>
> It's a bad way of doing it, but could you not create a unique index on the
> particular key you want to be unique, and then just try the select anyway
> with the mysql_query() call preceeded with an @ to suppress warnings?
>
> Thanks,
> Ash
>
> http://www.ashleysheridan.co.uk
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Does this list of inspections exist in the db? Could you not use an
INSERT INTO SELECT * FROM TABLE WHERE type statement...much less
processing overhead then

-- 

Bastien

Cat, the other other white meat

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux