Re: Compare and inserting with php
| From: | Miller, Terion | Date: | Tue, 30 Jun 2009 16:09:56 +0000 |
| Subject: | Re: Compare and inserting with php | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-294751@lists.php.net to get a copy of this message | ||
On 6/30/09 10:40 AM, "Bastien Koert" <phpster@gmail.com> wrote:
On Tue, Jun 30, 2009 at 11:17 AM, Ashley
Sheridan<ash@ashleysheridan.co.uk> 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
Hi Bastien,
Something like this is what you mean?
if (!empty($Go)) { $query = "SELECT * FROM restaurants WHERE name = '$ucName' AND
address = '$ucAddress' AND inDate ='$inDate' AND inType = '$inType'
"; $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()); }
So if all three things are met, that entry is in there if not insert right?